- Home /
2 instances of text with different colors using OnGUI() and GUI.Label?
I have the following code in OnGUI():
GUI.color = Color.yellow;
GUI.Label (new Rect ( 0, 0, 100, 23), "Score: " + gameScore);
GUI.color = Color.green;
GUI.Label (new Rect ( 100, 0, 120, 23), "Host Temp: " + fhostTemp);
But I don't get yellow text and green. If I want two separate colors how do I do this?
I would expect that to work.
Are you using a GUISkin that overrides the text color for a label?
Answer by WillTAtl · Nov 28, 2011 at 02:01 AM
Not sure what the top-level "color" attribute actually does in unity's GUI skins (I could check the docs, but not necessary to answer your question) (Edit: I checked. GUI.color sets a tint that is applied to all gui elements, including images as well as foreground and background colors; since the default gui skin's label text color is black, with the default skin this has no effect at all; if you change the skin's label text color to white, this should work as expected. It would apply that same color to any other gui elements unless reset to clear when you're done, though, so I still think this approach, particularly the second, is the better way to go, personally)
The setting you want to change is GUI.skin.label.normal.textColor - bit of a mouthfull, that's the text color for use in a "normal" state (as opposed to while active, focused, or hovering - none of which seem to apply to labels anyway), on the current skin used by GUI calls.
Example code, two versions:
function OnGUI() { //grab the label style for the current skin var labelStyle=GUI.skin.label;
//red text
labelStyle.normal.textColor=Color.red;
GUI.Label(Rect(0,0,100,30),"Red",labelStyle);
//since here I'm using a reference to modify the actual active skin,
//you can omit the skin parameter from the Label call, actually, as so...
//Yellow text
labelStyle.normal.textColor=Color.yellow;
GUI.Label(Rect(0,30,100,30),"Yellow"); //no need to pass it in
//you could also just modify it directly each time rather than caching a
//reference, as so...
//Blue text
GUI.skin.label.normal.textColor=Color.blue; //set it directly on the active
//skin in GUI
GUI.Label(Rect(0,60,100,30),"Blue"); //no need to pass it in
//note that this gui skin is a global object that persists between scenes,
//so if another object draws labels after this without explicitly setting
//the text color, it will use blue labels here. If it's another scene,
//it might draw blue labels when transitioning from this scene and another
//color when transitioning from another - not good! So consider the next
//possibility...
}
That'll do what you're trying to do, but there's another way which is perhaps better than doing this sort of thing entirely in code...
/*alternative approach*/
//I showed you the option of passing the style as a parameter as set up //for this alternative approach...
//three styles for three labels, definable in the inspector var labelStyle1:GUIStyle; var labelStyle2:GUIStyle; var labelStyle3:GUIStyle;
function OnGUI() {
GUI.Label(Rect(0,0,100,30),"Label 1",labelStyle1);
GUI.Label(Rect(0,30,100,30),"Label 2",labelStyle2);
GUI.Label(Rect(0,60,100,30),"Label 3",labelStyle3);
//this approach avoids the "sticky color" property I described before,
//and has the bonus of letting you more easily change any number of style
//properties directly in the inspector and see the results faster than you
//would when changing hard-coded script values.
}