yield WaitForSeconds in OnGUI

I have a GUI button. If I press the button, I have to wait for 1 second then should go to Level 1.

But now my problem is that I can’t use yield WaitForSeconds(1);.
It gives me this error.

This is by the way my JS:

var gui01 : Texture2D;
var gui01rollover : Texture2D;

private var correctedMousePosition : Vector2;
 
function OnGUI ()
{
    correctedMousePosition = Vector2 (Input.mousePosition.x, (Screen.height-Input.mousePosition.y) );
    if ( Rect(101,303,88,88).Contains(correctedMousePosition) )
    {
    	GUI.DrawTexture( Rect(101,303,88,88) , gui01rollover);
    	if( Input.GetMouseButtonUp(0) )
    	{
    		yield WaitForSeconds(1);
    		Application.LoadLevel("S1_level01");
    	}
    }
    else
    {
    	GUI.DrawTexture( Rect(101,303,88,88) , gui01);
    }
}

And this is the error Unity3D gives me;
Script error: OnGUI() can not be a coroutine.

Can someone help me with this problem please?

Thanks in advance.

Here’s a similar question i answered, but instead of ongui, it’s update there.

OnGUI runs every frame like Update, and can’t be delayed in any way. You can launch a separate coroutine from OnGUI instead (but you must take steps to ensure that it will only be launched once).

I’ve modified some code for you. A very basic way to solve this is to add a boolean (in this example I called it loadingLevel, and set it to false. Then when you add a simple if statement, that says if loadingLevel is false, then it is okay to enter the coroutine LoadLevel(), but before doing so, we set the loadingLevel boolean to true, this way the next time OnGUI runs, it does not enter that if statement, thus calling the LoadLevel() function again. Then the LoadLevel() function simply waits 1 second, then loads the level you specify.

var gui01 : Texture2D;
var gui01rollover : Texture2D;
var loadingLevel : boolean = false;
 
private var correctedMousePosition : Vector2;
 
function OnGUI ()
{
    correctedMousePosition = Vector2 (Input.mousePosition.x, (Screen.height-Input.mousePosition.y) );
    if ( Rect(101,303,88,88).Contains(correctedMousePosition) )
    {
        GUI.DrawTexture( Rect(101,303,88,88) , gui01rollover);
        if( Input.GetMouseButtonUp(0) )
        {
           if (!loadingLevel)
           {
              loadingLevel = true;
              LoadLevel ();
           }
        }
    }
    else
    {
        GUI.DrawTexture( Rect(101,303,88,88) , gui01);
    }
}

function LoadLevel () {
    yield WaitForSeconds(1);
    Application.LoadLevel("S1_level01");
}

I fixed my problem. For those who have the same problem I had with OnGUI. You can find the right JS here.

var gui01 : Texture2D;
var gui01rollover : Texture2D;

private var correctedMousePosition : Vector2;

function OnGUI ()
{
    correctedMousePosition = Vector2 (Input.mousePosition.x, (Screen.height-Input.mousePosition.y) );
    if ( Rect(101,303,88,88).Contains(correctedMousePosition) )
    {
    	GUI.DrawTexture( Rect(101,303,88,88) , gui01rollover);
    	if( Input.GetMouseButtonUp(0) )
    	{
    		GoScene();
    	}
    }
    else
    {
    	GUI.DrawTexture( Rect(101,303,88,88) , gui01);
    }
}

function GoScene()
{
	yield WaitForSeconds(1);
	Application.LoadLevel("S1_level01");
}