How to make a Label a variable

I have been attempting to make a GUI Label a variable - but it always errors… Am I doing anything wrong?

using UnityEngine;
using System.Collections;

public class clue : MonoBehaviour {

	void Start () {
		Screen.SetResolution(1024, 768, true);
	}
	
	void Update () {

	}
	void OnGUI () {
		// Main Menu.
		var height =  Screen.height;
		var width  =  Screen.width;
        var label =  GUI.Label (new Rect (width/2, height/2, 100, 200), "Menu:");


	}
}

I don't think you can use it as a variable, it doesn't return anything and it's not a class itself or variable type. What are you trying to do with it?

–

I will be wanting to get rid of it after about 5 seconds so I thought I would assign it as a variable.

–

Oh, I think you'll have to set it up with a timer/coroutine to turn it on and off, I don't know any other ways of doing it.

–

Thanks, for the information.

–

GUI.Label is a call you make inside of OnGUI(). its not persistant, it just causes the label to be drawn that one time. if the next time OnGUI() is called, you don't call GUI.Label, then your label will disappear.

–

1 Answer

1

If you wish to turn it off after 5 seconds, the easiest way would be to use a boolean flag and turn it off.

You can use a coroutine or Invoke to set the flag off after some seconds.

void Start () {

	Invoke("turnLabelOff",5f);
}

bool flag = true;
void OnGUI()
{
	if(flag)
	{
		GUI.Label(new Rect(0f,0f,100f,20f),"Off in 5 seconds");
	}
}

void turnLabelOff()
{
	flag = false;
}