Search Unity

Changing the color of a UI Button with scripting?

Discussion in 'UGUI & TextMesh Pro' started by TheFacelessOne, Sep 8, 2014.

  1. TheFacelessOne

    TheFacelessOne

    Joined:
    Aug 19, 2014
    Posts:
    5
    I'm trying to change the color of a button depending on an array. Is there any way I can dynamically change the color of a button? It should be under the image class, but it's not.

    If not, can I do a similar procedure, but with sprites? In the future, I plan to change the sprite based on the array, but until then I want to just use colors for debugging. Or I could just add dumb images.

    Problem solved, was easy. I just didn't think it through.
     
    Last edited: Sep 9, 2014
    JustATensai and faalbane like this.
  2. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    You should definitely be able to set Image.color. I'm doing it right now.
     
    thewizdom likes this.
  3. ninjapretzel

    ninjapretzel

    Joined:
    Jul 19, 2011
    Posts:
    27
    put this in the start method of some C# script:
    Code (csharp):
    1.  
    2. GetComponent<Image>().color = Color.red;
    3.  
    should recolor the button red when the script starts.

    just access the color property of the Image component on the button, and set it to whatever color you want.
     
  4. TheFacelessOne

    TheFacelessOne

    Joined:
    Aug 19, 2014
    Posts:
    5
    Thanks a ton. I don't know why I didn't try that. I feel like a dork.
     
    DavidPaulinus likes this.
  5. emssik

    emssik

    Joined:
    Aug 8, 2014
    Posts:
    2
    This is easy one.

    How to change for example "Normal Color" if Transition is ColorTint?

    If you set new color to the Image (like in posts before) then you loose the higlight color, etc.
     
    sonnyb likes this.
  6. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,225
    Code (csharp):
    1.  
    2.         var colors = GetComponent<Button> ().colors;
    3.         colors.normalColor = Color.red;
    4.         GetComponent<Button> ().colors = colors;
    5.  
     
  7. emssik

    emssik

    Joined:
    Aug 8, 2014
    Posts:
    2
    Thanks. That was a code which I was looking for ;)
     
  8. Abhi94

    Abhi94

    Joined:
    Apr 4, 2015
    Posts:
    2
    thanks it really helped .
    how can i do this for displaying random colours?
     
  9. Ramcat

    Ramcat

    Joined:
    Aug 16, 2014
    Posts:
    95
    Do this but use random numbers:
    Code (CSharp):
    1. _This = this.GetComponent<Image>();
    2. _This.color = new Color(255, 0, 0);
     
  10. takito

    takito

    Joined:
    Apr 15, 2013
    Posts:
    11
    This is how I normally go about doing this...

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class ColorActivity : MonoBehaviour {
    6.     //Reference to button to access its components
    7.     private Button theButton;
    8.  
    9.     //this get the Transitions of the Button as its pressed
    10.     private ColorBlock theColor;
    11.  
    12.     // Use this for initialization
    13.     void Awake () {
    14.         theButton = GetComponent<Button>();
    15.         theColor = GetComponent<Button>().colors;
    16.  
    17.     }
    18.  
    19.  
    20.     /// <summary>
    21.     /// Example
    22.     /// just add this to your Button component On Click()
    23.     /// </summary>
    24.     public void ButtonTransitionColors()
    25.     {
    26.  
    27.         theColor.highlightedColor = Color.blue;
    28.         theColor.normalColor = Color.cyan;
    29.         theColor.pressedColor = Color.green;
    30.  
    31.         theButton.colors = theColor;
    32.         print("Cliked");
    33.     }
    34. }
    35.  
    then just add it to the button on click
    hope it helps
     
  11. ahmadian

    ahmadian

    Joined:
    Nov 24, 2015
    Posts:
    44
    this is not working . can anyone help?
     
  12. ahmadian

    ahmadian

    Joined:
    Nov 24, 2015
    Posts:
    44
    Sorry false alarm :rolleyes:
     
  13. dh151810

    dh151810

    Joined:
    Oct 11, 2015
    Posts:
    2
    I'd suggest to use Color32(...), because it works with bytes (0-255) ,whereas Color(...) expects values between 0-1.

    Code (CSharp):
    1. _This.color=new Color32(255,0,0);
     
  14. wasicool7

    wasicool7

    Joined:
    Jul 28, 2016
    Posts:
    25
    Does any one here knows how to change image color through time, so that when I change the image color it doesn't change automatically, instead it changes slowly! Thank you :)
     
  15. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @wasicool7

    Hi there,

    just animate button's Image component color, by changing it over time.... it will affect whole button color.

    Or alternatively animate Button component colors, these are contained in ColorBlock, which you can get, update and then put back, so you can animate normal color only, for example.

    You could use animation clip, curve + time, sin wave - whatever is right for your purpose.
     
  16. zacharypicco

    zacharypicco

    Joined:
    Nov 26, 2017
    Posts:
    1
    As a side note, this is a very easy way to make UI elements invisible
     
  17. naviln

    naviln

    Joined:
    Oct 18, 2016
    Posts:
    32
    Thanks for this
     
  18. OfficeThrashing

    OfficeThrashing

    Joined:
    May 18, 2018
    Posts:
    13
    Perfect!!!
     
  19. surajgupta052

    surajgupta052

    Joined:
    Apr 3, 2019
    Posts:
    3
     
  20. surajgupta052

    surajgupta052

    Joined:
    Apr 3, 2019
    Posts:
    3
    How to change the Image colour in canvas on being pressing button???
     
  21. surajgupta052

    surajgupta052

    Joined:
    Apr 3, 2019
    Posts:
    3
    Sir can u help me in changing the colour of Image in Canvas on pressing button??
     
  22. gurkan

    gurkan

    Joined:
    Jul 4, 2013
    Posts:
    23
    this code:
    Code (CSharp):
    1. mybutton.highlightedColor = Color.cyan;
    2. mybutton.normalColor = Color.cyan;
    3. mybutton.pressedColor = Color.green;
    4. mybutton.colorMultiplier = 1;
     
  23. wingman103

    wingman103

    Joined:
    Oct 18, 2014
    Posts:
    3
    i will show how to write in hexcode..!!
    Code (CSharp):
    1. If you wanna write in Hexcode;
    2. if(i want hexcode)
    3. { button.color=mydesiredcolored;
    4. }
     
  24. naklow12

    naklow12

    Joined:
    Jun 14, 2017
    Posts:
    8
    Hello guys, I want to update the topic because of changes. If you guys are adding as "Image" component, changing color won't work. So you guys should add as "Graphic" component.

    For UI Image element:
    Code (CSharp):
    1. gameObject.GetComponent("Graphic").color = Color.red;
    will work.
     
    Tankzo likes this.
  25. duartejuca

    duartejuca

    Joined:
    Aug 23, 2018
    Posts:
    13
    Code (CSharp):
    1. mybutton = // find button somewhere or point reference;
    2.  
    3. newColor = Color.white; // or any other color
    4. ColorBlock colors = ColorBlock.defaultColorBlock;
    5. colors.highlightedColor = newColor ;
    6. mybutton .GetComponent<Button>().colors = colors;
     
  26. giantkilleroverunity3d

    giantkilleroverunity3d

    Joined:
    Feb 28, 2014
    Posts:
    383
    This what I am trying:
    Code (CSharp):
    1.         m_TextGreen.color = Color.green;//Works
    2.         m_TextGreen.color = new Color(135.0f, 245.0f, 15.0f, 255.0f);//Doesnt work
    3.        
     
    yormanh likes this.
  27. giantkilleroverunity3d

    giantkilleroverunity3d

    Joined:
    Feb 28, 2014
    Posts:
    383
    I just posted a different version of this problem. I need to use my own colors.
    In Unity 2018.3
     
  28. naviln

    naviln

    Joined:
    Oct 18, 2016
    Posts:
    32
    Hey buddy, try using ColorUtility.TryParseHtmlString, I use something like this:

    Code (CSharp):
    1. ColorUtility.TryParseHtmlString("#C68B8BFF", out Color defaultCardColor);
    2.                 cardArt.color = defaultCardColor;
    You can feed in your own custom color codes there
     
  29. Tankzo

    Tankzo

    Joined:
    Jul 16, 2019
    Posts:
    16
    This was the exact solution I was looking for (and it works 11-years later.

    Code (CSharp):
    1. public Button button0;
    2.  
    3.     private ColorBlock originalColorBlock;
    4.     private ColorBlock pressedColorBlock;
    5. private void Start()
    6.     {
    7.         originalColorBlock = ColorBlock.defaultColorBlock;
    8.         //Need to assign the "pressed" to the default colors otherwise when releasing the key, the button "flickers" to black.
    9.         //This is because with only setting the "normalColor" the other colors are set to 0,0,0,1 or black.
    10.         pressedColorBlock = ColorBlock.defaultColorBlock;
    11.         pressedColorBlock.normalColor = Color.gray;
    12.     }
    13.  
    14.     private void Update()
    15.     {
    16.  
    17.         if (Input.GetKey(KeyCode.Alpha0) || Input.GetKey(KeyCode.Keypad0))
    18.         {
    19.             button0.colors = pressedColorBlock;
    20.         }
    21.         else if (Input.GetKeyUp(KeyCode.Alpha0) || Input.GetKeyUp(KeyCode.Keypad0))
    22.         {
    23.             button0.colors = originalColorBlock;
    24.         }
    25.     }
     
  30. Noob_Master_XD

    Noob_Master_XD

    Joined:
    Jan 18, 2022
    Posts:
    2
    Can someone help me with adding random colors to unity's button script, im making a game for my 1 year old sis
     
  31. unity_2354966805AA06F15CD1

    unity_2354966805AA06F15CD1

    Joined:
    Oct 11, 2022
    Posts:
    1
    yeah by replicating the basic square and using c# reverse techniqe
     
  32. TsukiAkurei

    TsukiAkurei

    Joined:
    Sep 2, 2017
    Posts:
    1
    Because this is one of the first Google results, with no clear solution in 2024:

    Unity's Color constructor expects a value between 0 and 1, so passing any sort of RGB value from 0 to 255 just doesn't work.

    To fix it, divide your custom color's RGB values by 255f, and viola:

    Code (CSharp):
    1. // Divide all the RGB values by 255f to get a value between 0 and 1
    2.         m_TextGreen.color = new Color(135f / 255f, 245f / 255f, 15f / 255f, 255f / 255f);