Custom font in GUI.Label, but can't change it's color

Hi, I followed the Michael La Voie answer from this post: http://answers.unity3d.com/questions/6585/change-gui-font-size-and-color , but I can't change font color. By default the shader of an imported font is read-only and I can't manually change the color, though here are 2 script attempts which failed:

  • GUI.color = Color.red;
  • myStyle.font.material.color = Color.red;

6 Answers

6

I came to the same problem and found a simple solution. This is JavaScript but sure you get the point if you want to use C#:

var TextStyle = new GUIStyle();

function OnGUI ()
{       
    GUI.Label (Rect (10, 10, 200, 80), "This is a styled text", TextStyle);
}

Add the above script to any GameObject. The script component will now publish a property TextStyle, where you can edit and control all of its properties at design time:

  • TextStyle -> Normal -> Text Color for setting the text color.
  • TextStyle -> Font for specifying any font (drag & drop from your Assets or choose from the drop-down menu).

Of course, you can also set the properties at runtime:

function OnStart ()
{
    TextStyle.normal.textColor = Color.red;
    TextStyle.font = myFont;
}

To clarify, since this is an old answer: If you're working with a GUIStyle (so you can set fontsize, etc.), the line of code you need to change the color is: myGUIStyle.normal.textColor = Color.red; // ( or Color.white or Color.whateverYouWant ) Setting colors on the GUI object (e.g. GUI.contentColor), as suggested by most of the other answers, WON'T work if your text is being displayed with a GUIStyle.

I think you want GUI.contentColor, e.g.

GUI.contentColor = Color.red;

doesn't work either...

In a clean project with default styles that works, so the issue must be something else. To be clear, you set the GUI.color or GUI.contentColor right before drawing the text with GUI/GUILayout. The color is multiplied into your text color, so if your GUIStyle uses a black color, it will stay black -- make sure the style is using white.

Try this in your start function:

TextStyle.normal.textColor = Color.red;

As of Unity 4, this works:

GUI.color = Color.red

do like this:
gui.label(rect, “gui_color_label”)

This should work:

var TextColor : Color = Color.white;

function Start () {
	guiText.material.color = TextColor; 
}

The question is about OnGUI code, not GUIText objects.