1

I am having a problem this morning with my Button.Image.Color

here is my really simple code :

this.gameObject.GetComponent<Image>().color = Color.Red;

So this line perfectly works...Now, I am trying to use my own color onto that button with this, again, simple line of code :

this.gameObject.GetComponent<Image>().color = new Color(106, 174, 23);

Well, the color actually changes...without changing...let's refer to the object itself while the game is running :

My button / my color

So as you can see, the color changed but, it stays white.

I've looked at many posts in here but it appears that I am doing it right, obviously there is a mistake or else it would work fine, but even after reading a lot of posts it seems that I cannot see the solution on my own.

2 Answers2

3

Color accepts floats ranging from 0 - 1 for it's constructors.

You are setting them to 1 (anything above 1 is set to 1). And 1,1,1 = white.

this.gameObject.GetComponent<Image>().color = new Color(0.415f, 0.682f, 0.09f);

This should give you the result you are looking for.

The numbers are generated by dividing your values by 255

Alternatively you could also parse your hexvalue:

if(Color.TryParseHtmlString("#75BF1A", out myColor))
{
    this.gameObject.GetComponent<Image>().color = myColor;
}
Immorality
  • 2,164
  • 2
  • 12
  • 24
1

Hello and thanks for the answer, indeed Color needs number from 0 to 1.

Just to add to your answer I found that Color32 was the one I should have used since it takes the 0 - 255 values.

Thanks again have a great day!