How to change Normal color, Highlighted color etc. in 4.6 buttons with code

The new 4.6 buttons have options for Highlighted color, Normal color, Pressed color, Disabled color. I want to change those with code.

When I try something like

tempButton.GetComponent<Button>().colors.normalColor = Color.red;

I get an error: Assets/2-scripts/LevelSelectMenuScript.cs(32,49): error CS1612: Cannot modify a value type return value of `UnityEngine.UI.Selectable.colors’. Consider storing the value in a temporary variable

When I try something like

tempButton.GetComponent<Button>().image.color = Color.red;

I get a weird mix of the image color (from the image component) and the normal color (from the button component)

Any ideas on how to do it right?

1 Like

Ahh - I had exactly this question. The hint you provided got me moving.

I found that:

Button b = rButton.GetComponent<Button>(); 
ColorBlock cb = b.colors;
cb.normalColor = Color.white;
b.colors = cb;

does the trick. (rButton is the Button game object.)

Button.colors.normalColor