Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

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:
    26
    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.
     
  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.
     
  6. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,160
    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:
    92
    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:
    10
    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,236
    @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:
    31
    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:
    21
    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:
    2
    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:
    3
    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.
     
  25. duartejuca

    duartejuca

    Joined:
    Aug 23, 2018
    Posts:
    4
    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:
    160
    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.        
     
  27. giantkilleroverunity3d

    giantkilleroverunity3d

    Joined:
    Feb 28, 2014
    Posts:
    160
    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:
    31
    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
     
unityunity