Search Unity

GUI.Label Color won't change

Discussion in 'Immediate Mode GUI (IMGUI)' started by JordanRoss, Mar 2, 2017.

  1. JordanRoss

    JordanRoss

    Joined:
    Nov 25, 2013
    Posts:
    9
    Hello, I'm trying to get an UI element to change the font colour after making it change font size, here is my code below, what could I be doing wrong for it not to change?

    Code (CSharp):
    1. private GUIStyle guiStyle = new GUIStyle();
    2.  
    3. public void OnGUI()
    4.     {
    5.         GUI.contentColor = Color.white;
    6.         guiStyle.fontSize = 30;
    7.         GUI.Label (new Rect (620, 200, 400, 100), attempts.ToString(), guiStyle);
    8.     }
    9.  
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,407
    no idea, but at least this works:
    Code (CSharp):
    1. GUI.Label(new Rect(620, 200, 400, 100), "<color=green>asdfasdfasdf</color>".ToString(), guiStyle);
    2.  
     
  3. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,692
    GUI.contentColor specifies the tinting. Since white is (1,1,1,1), this means it will just show the current style's color at full value.

    You'll want to change the GUIStyle's color instead. Something like:
    Code (csharp):
    1. guiStyle.fontSize = 30;
    2. guiStyle.normal.textColor = Color.white;
    3. GUI.Label (new Rect (620, 200, 400, 100), attempts.ToString(), guiStyle);
    Side note: If this is for runtime code, you may want to cache the return value of attempts.ToString() instead of computing it in every OnGUI() call.