Search Unity

GUI.Label coloring?

Discussion in 'Scripting' started by pjmiller435, Dec 23, 2014.

  1. pjmiller435

    pjmiller435

    Joined:
    Dec 6, 2013
    Posts:
    4
    Ok, so I have a bool turning true upon clicking of a button, and onGUI I of course have an if statement that when said bool is true to display a label, i.e.

    Code (CSharp):
    1. GUI.Label(new Rect((Screen.width / 2 - 100), (Screen.height / 2), 200, 200), "Text Here!");
    But how do I color said text and only said text? I know there's GUI.Color, etc. but that changes the whole GUI color. I'm looking for just setting a custom color to text on-screen. Not sure if it's possible with GUI.Label or I use something else? Or try maybe the new UI added in 4.6?
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Code (CSharp):
    1. GUI.color = xxx
    2. GUI.label ..
    3. GUI.color = Color.white
     
    pjmiller435 likes this.
  3. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    If you use the 4.6 UI system its trivial. Remove OnGUI and just use the Canvas that gets created with the new UI built from Unity. To create a 'Label' in your GUI script create a field of type 'Text' then drag the 'Text' that you created in Unity via GameObject->UI->Text drag it in here and you can change the color like so.

    Code (CSharp):
    1. using UnityEngine.UI; // New Library for UI in 4.6
    2.  
    3. public class MyGUI : Monobehavior {
    4.      [SerializeField]
    5.      Text myText;
    6.  
    7.      void Start() {
    8.             this.myText.color = Color.blue; // Change color here
    9.      }
    10. }
    hope this helps
     
    pjmiller435 likes this.
  4. pjmiller435

    pjmiller435

    Joined:
    Dec 6, 2013
    Posts:
    4
    Thanks a lot, it actually did help cause I've never even explored the new UI features. Thanks for peaking my interest in it :p
     
  5. pjmiller435

    pjmiller435

    Joined:
    Dec 6, 2013
    Posts:
    4
    Hmm could be wrong but just thinking wouldn't this again change the entire color of my GUI for a brief half-second and then turn it back to white/default color I have? Was looking more for a way to change the color of some text on the screen and only that text and to keep it that color throughout (while bool is true).
     
  6. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Thats not how UnityGUI works
    you know you could have just tried it, its 3 lines of code, and you'd see that theres no half-second color changes
     
    Mycroft likes this.
  7. pjmiller435

    pjmiller435

    Joined:
    Dec 6, 2013
    Posts:
    4
    I was at work when I replied lol.