Temporary GUI

I’m working on a small bit of code right now to have something show up on the screen but only for 3 seconds. I’ve gotten this far, but when I run the code it says “Script Error OnGUI () cannot be a coroutine.” The debug code does not trigger either.

function OnGUI () {
	if (Collect == true) {
		GUI.Label (Rect (10, 10, 100, 20), Gem + (" gems found"));
		Debug.Log ("On");
		yield WaitForSeconds (3);
		Collect = false;
		Debug.Log ("Off");
	}
}

So, to anyone who knows what I’m doing wrong: Are you kind enough to share your knowledge with me?

You can’t put yield into functions like OnGUI or Update but you could do something like this:

function OnGUI () {
if (Collect == true) {
GUI.Label (Rect (10, 10, 100, 20), Gem + (" gems found"));
Debug.Log ("On");
}
}

function Only3Seconds(){
   Collect = true;
   yield WaitForSeconds (3);
   Collect = false;
   Debug.Log ("Off");
}

If you then want to start it you use this line:

yield StartCoroutine("Only3Seconds");

try this:

var Collect : boolean;

function OnGUI()
{
    if(Collect == true)
    //Do your Things here
}

function Update()
{
    if(Collect == true)
    {
        RemoveGUI();
    }
}

function RemoveGUI()
{
    yield WaitForSeconds(3);
    Collect = false;
}