Change the Colour of UGUI button in script?

For the new UGUI button. I want to change the colour when the button has been pressed and then when a new button is pressed I want to be able to change the colour of new button and reset the old button’s colour - kind of like a radio button. However I don’t know how to access the UGUI button colour in script.

2 Answers

2

You need to add:

using UnityEngine.UI;

In the top of your script. Then you can access all the UI related classes.

public Button button;

public void Start()
{
   button.colors.normalColor = new Color(0.22f, 0.22f, 0.22f, 1f);
}

use this: Button button_1 for your variable. button_1.gameObject.onClick .... button_1.colors ...

I ended up using this: button_1.GetComponent<Button>().image.color = Color.red; Which works but I would still like to actually change the normal color in case I am ever need too.

This is what I did to change the color of the new UI images in 4.6 beta 17, hope it helps =)

using UnityEngine.UI;

    public (name of script that holds the color variable lets say Image) Image image;
    public Transform Button; (either drag the button onto this or use the Start function)
    public Color color;
    
    void Start()
    {
         Button = GameObject.Find("button");
    }
    
    void Update()
    {
         //If pressed == true then do the following:
         image = Button.GetComponent<Image>();
    
         color.r = 1; (put percent out of 1 for amount of red on button i.e. I want it all red)
         color.g = 0.1f; (with a little bit of green, 10% green)
         color.b = 0.1f; (and a little bit of blue, 10% blue)
         color.a = 0.5f; (and the alpha at 50%)
    
         image.color = color;
    }