GUI Text disappearing and Coroutines

I’m new to java scripting and game design in general, so I can’t seem to figure out the issue with this script I wrote.

var clicked = false;
var time = true;
var seconds = 5;

function Start () 
{
    clicked = false;
    time = true;
    if (gameObject.GetComponent(Collider) == null)
    {
       gameObject.AddComponent(typeof(BoxCollider));
    }
}
 
function OnMouseDown()
{
    clicked = true;
}
 
function OnMouseUp()
{
    time = false; 
}
 
function Update() 
{
    if (clicked == true)
    {
       GUI.Label(new Rect(350,250,400,100), "Door seems to be blocked..");
    }

{
    if (time == false) 
    {
    	yield WaitForSeconds(seconds);
    	clicked = false;
    	}
	}
}

I can run the game, and it says that the variable “time” is true when the game loads and then false when the game object I put the script on is clicked. Likewise, the “isClicked” variable is checked in the inspector when the gameobject is clicked, but there is no countdown of the 5 seconds. I’m also getting an error about Update() cannot be used as a Coroutine. I looked on the unity wiki and saw that functions run over a period of a single frame while Coroutines kind of spread the time out, which makes sense because I’m using a script that takes place over a period of time. But I can’t figure out how to use a Coroutine. Any help would be appreciated.

You have two major problems here. First, you cannot use ‘yield’ inside of Update(). Second, GUI functions like GUI.Label() must be executed inside of the OnGUI callback function. I had a guess a bit about what you want to happen with this code, but I think you want:

#pragma strict

var clicked = false;
var seconds = 5;

function Start ()  {
    clicked = false;
    
    if (gameObject.GetComponent(Collider) == null) {
       gameObject.AddComponent(typeof(BoxCollider));
    }
}

function OnMouseDown() {
    clicked = true;
}

function OnMouseUp() {
    yield WaitForSeconds(seconds);
    clicked = false;
}

function OnGUI() {
	if (clicked) {
		GUI.Label(new Rect(350,250,400,100), "Door seems to be blocked..");
	}
}