How do i change the color of a button through a script(4.6)?

I’m curious as to how you can change to color of a button through a script in the new UI in Unity 4.6.

You have two ways to do this :

  1. Change the button’s image color
  2. Change transition’s color tint

Examples :

(1)

using UnityEngine.UI;
//...
Color buttonImageColor = gameObject.GetComponent<Image>().color;
buttonImageColor = new Color(1, 1, 1, 0.5f); // Some color

(2)

using UnityEngine.UI;
//...
ColorBlock colorTint = gameObject.GetComponent<Button>().colors;
colorTint.normalColor = new Color(1, 1, 1, 0.5f); // Some color

Note :

The 2nd way alters the button’s image to allow color variance when hovering the button, click it, etc…

You will find some tutorials here and here.
Hope it helps :slight_smile: