Change the color of instantiated button

Updated on January 15, 2017 in [A] GUI
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
7 on January 15, 2017

I am instantiating a button as a child of a rect transform with the scrolling thing(very good explanation, go me).

When I instantiate the button I am getting the text component of the child to change the text.
I wanted to do the same thing for the color, find the image component of the button and change the color, but the problem is that the color isn’t changing. I even tried to change it at runtime but it doesn’t change even then.

Here is a sample code from my script:


Button playerButton = (Button)Instantiate(buttonPrefab, buttonsParent);

Text playerButtonText = playerButton.GetComponentInChildren<Text>();

Image playerButtonImage = playerButton.GetComponent<Image>();

playerButtonText.text = player.playerName;

playerButtonImage.color = player.color;

  • Liked by
Reply
6 on January 15, 2017

The button component overrides the color of the image.
You can see that in the inspector.
 
So either make the button not animate the color, or set the color of the button component to your value.

Devoted
on January 15, 2017

I am using a animation to animate the button, I am animating it’s scale, only when it is disabled I animate it’s color.
How do I set the color of the button component, I tried it like this:

playerButton.color

but I only get the colors property of the playerButton which has a description of
“The ColorBlock for this selectable object.”

Guru
on January 15, 2017
Devoted
on January 15, 2017

It says: “Modifications will not be visible if transition is not ColorTint.” I am not using color tint, I am using animations (with the animator controller…)

Guru
on January 15, 2017

Hmmm, then the way you did it should work…
Are you sure the player color is what you expect it to be?
Make sure it’s not just white.

Devoted
on January 15, 2017

I even tried using different types of declaring the color but it still doesn’t work

playerButtonImage.color = Color.red;

and

playerButtonImage.color = new Color(244.0f / 255.0f, 67.0f / 255.0f, 54.0f / 255.0f, 1f);
Devoted
on January 15, 2017

I even tried turning off the transitions completely, same results.

Show more replies
  • Liked by
Reply
Cancel