Home > Software engineering >  How do you use Color32 in Unity C#? I tried to find out how, but it doesn't seem to work
How do you use Color32 in Unity C#? I tried to find out how, but it doesn't seem to work

Time:06-14

I tried a lot and I am trying to use RGBA values in my game. I don't really have much experience with C# and am only using it for unity. I basically need to change the color of a cube. When using basic colors like below, it works, fine, the cube turns red when this code applies.

 GetComponent<Renderer>().material.color = Color.red;

But I wanted a darker shade of red and more customizable colors, so I looked it up and after searching through a website, I found out about Color32. I tried the code below, and it doesn't work, though the cube turns white instead of the specified RGBA value:

 GetComponent<Renderer>().material.color = new Color32(139,0,0, 255);

I have no console errors with this code, so I'm thinking it might be the wrong way to do this. Any ideas??

CodePudding user response:

Color uses a range from 0 to 1 for the component values.

You can construct the color using the constructor.

var darkRed = new Color(0.55f, 0, 0, 1f);

I tested Color32 using the code from your question and it worked correctly. It is unclear how to reproduce the issue.

CodePudding user response:

Use the Color class, which has an inherent RGB/ARGB setter.

Color customColor = Color.FromArgb(alpha, red, green, blue);
GetComponent<Renderer>().material.color = customColor;
  • Related