• Register
Post tutorial RSS Unity GUI for Beginners: GUI.Label

Simple JavaScript and C-Sharp examples of how to add a label on-screen in your Unity game. I will do more advanced tutorials later, but am spending much time developing at the moment. This is for beginners with little experience coding in Unity. This example will allow you to add a score label, along with a score converted to a string. You will set the Rectangle (Rect) of the label PUBLIC so it can be set in inspector, the same goes for the STYLE to change font color, size, etc...

Posted by on - Basic Client Side Coding

using UnityEngine;
using System.Collections;

public class EXAMPLE : MonoBehaviour
{
    public Rect labelPosition;
    string labelText;
    public GUIStyle labelStyle;
    int score = 0;

    //Set labelPosition public so it can be changed in inspector (editor). 

    void Update()
    {
        score+=1;
        labelText = "Score - " + score.ToString();
    }

    void OnGUI()
    {
        GUI.Label(labelPosition, labelText, labelStyle);
    }
}


END OF CSHARP CODE


#pragma strict

var labelPosition : Rect;
var labelText : String;
var labelStyle : GUIStyle;
var score : int;

function Update()
{
    score+=1;
    labelText = "Score - " + score.ToString();
}

function OnGUI()
{
    GUI.Label(labelPosition, labelText, labelStyle);
}

END OF JS CODE

[spoiler][/spoiler]

Post a comment
Sign in or join with:

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.