• Register

Unity is a multiplatform game development tool, designed from the start to ease creation. A fully integrated professional application, Unity just happens to contain the most powerful engine this side of a million dollars.

Report 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

Your comment will be anonymous unless you join the community. Or sign in with your social account: