How to I change my font size and color with Unity GUI?

2.1k Views Asked by At

I am working on a project in Unity and I'm trying to change my font size and color of my label. I keep getting a error on line 21... I think it's not reading the colon or something. How do I fix?

public class PlayerScore : MonoBehaviour
{
    public int points = 0;

    // Start is called before the first frame update
    void Start()
    { }

    // Update is called once per frame
    void Update()
    { }


    // score label
    private void OnGUI() 
    {
        var myFont : GUIStyle = new GUIStyle();
        myFont.fontSize = 100;
        GUI.color = Color.white;
        GUI.Label(new Rect(10, 10, 100, 100),  "Score: ", + points);
    }
}

Screenshot of code and errors here

3

There are 3 best solutions below

0
On

I think that you should do var myFont : GUIStyle; instead. After that you can modify the style in the inspector.

Try to give a better read on that page on Unity manual on how to use it accordingly: https://docs.unity3d.com/Manual/class-GUIStyle.html

0
On

The colon is what's giving you the compilation error C# syntax is

var myFont = new GuiStyle();

or

GuiStyle myFont = new GuiStyle;

or

GuiStyle myFont = new();

Sorry - I know this is 6 months late but might help others coming this way (like me!)

And in order to change the label colour - this is working code:

GUIStyle style = new();
style.normal.textColor = Color.black;
Handles.Label(position, "your text", style);
0
On

Or

myFont.normal.textColor = new Color(0.9f, 0.7f, 0.5f);

I mean, any of color you want, this is yellow one.