- Home /
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.
Answer by Freaking-Pingo · Sep 23, 2014 at 08:07 PM
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);
 }
Ok So Obviously I'm still missing something because I had all of my buttons listed as GameObjects since I couldn't add the listeners properly when they were Buttons. So this is what I have:
 GameObject button;
 button_1 = GameObject.Find ("button_1");
   button_1.GetComponent().onClick.AddListener(delegate{selectRollNumber(1);});
 button_1.GetComponent<Button>().colors.normalColor = new   Color(0.22f, 0.22f, 0.22f, 1f);
And I get the error "Assets/Scripts/DieRollerScript.cs(165,49): error CS1612: Cannot modify a value type return value of `UnityEngine.UI.Selectable.colors'. Consider storing the value in a temporary variable"
I'm not sure what I'm doing wrong. I expect I've over complicated something.
use this:
Button button_1 for your variable.
button_1.gameObject.onClick ....
button_1.colors ...
I ended up using this: button_1.GetComponent().image.color = Color.red; Which works but I would still like to actually change the normal color in case I am ever need too.
Answer by jjkiii · Sep 24, 2014 at 03:53 AM
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;
     }
     
 
 
Your answer
 
 
             Follow this Question
Related Questions
Changing gameobject material color makes object disappear from view. How can I fix this? 1 Answer
Is this code enough to crash Unity? 1 Answer
Change the color of the Unity 4.6 buttons in code 2 Answers
guiTexture color half alpha White? 1 Answer
How to have correct color on imGUI Buttons (as dynamic textures) avoiding multiply effect ? 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                