- Home /
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.
Answer by YoungDeveloper · Sep 22, 2013 at 08:28 PM
Here's a similar question i answered, but instead of ongui, it's update there. http://answers.unity3d.com/questions/541018/wow-my-enemies-are-pretty-strong.html
Answer by Eric5h5 · Sep 22, 2013 at 08:07 PM
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).
Answer by ItsK4rma · Sep 22, 2013 at 08:48 PM
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");
}
Answer by androids · Sep 22, 2013 at 08:54 PM
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");
}
i couldn't able to understand. i header that waitforseconds can be used only in IEnumerator funtion, is the above code possible.
need help.
@galaboy: In the above code, WaitForSeconds is in an IEnumerator function.
Your answer
Follow this Question
Related Questions
Function On GUI 2 Answers
Difference between yield WaitForSeconds and yield new WaitForSeconds 1 Answer
Mysteries of yield 1 Answer