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

Viewed 164

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??

3 Answers

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.

if you make the Color as public then we can adjust the color in the inspector itself.So it will be easy to view the color you want.You can use the code in Update and see the changes in the editor by changing the color in inspector.

public class ColorTest : MonoBehaviour
{

public Color colourSelection;

// Start is called before the first frame update
void Start()
{
    //GetComponent<Renderer>().material.color = colourSelection;
}

void Update()
{
    GetComponent<Renderer>().material.color = colourSelection;
}
}

Check here

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