- Home /
Script seems to halt if a "yield waitForEndOfFrame" is included
Hey guys,
Basically, I'm scripting a GUI and I was trying to get one GUI to fade out before switching the active script to another GUI. I usually make a while loop like this with a "yield WaitForEndOfFrame" so it doesn't fade out at the speed of light.
function fadeToOptions ()
{
if(GUIBusy)
return;
GUIBusy = true;
GUI.color.a = 1.0;
while(GUI.color.a > 0)
{
GUI.color.a -= 0.02;
print("working" + GUI.color.a);
yield WaitForEndOfFrame();
}
options.enabled = true;
options.fadeIn();
this.enabled = false;
GUIBusy = false;
}
Basically, if I include that yield in that while loop, the script seems to stop and not do anything. If I take it out it will switch to the other GUI just fine, but it won't really fade out because it goes way to fast without the yield. Anyone know what's going on?
Answer by Bunny83 · Mar 30, 2012 at 02:33 AM
Well, i never tried a coroutine-while-loop with just WaitForEndOfFrame, but i guess when you reached the end of the current frame and yield again until the EndOfFrame, it will return immediately since the coroutine handler is actually processing the EndOfFrame - stack / queue.
while(GUI.color.a > 0)
{
yield WaitForEndOfFrame();
GUI.color.a -= 0.02;
print("working" + GUI.color.a);
yield; // this waits for the next frame
}
But i don't quite understand why you need this piece of code at the end of frame?
Setting GUI.color outside OnGUI is pretty much useless. Also usually you aren't allowed to use any GUI stuff outside OnGUI. OnGUI is bound to some global states set by the engine (the Event class for example).
You could use a class variable that is manipulated in the coroutine and used in OnGUI.
"You could use a class variable that is manipulated in the coroutine and used in OnGUI."
An excellent idea!
" when you reached the end of the current frame and yield again until the EndOfFrame, it will return immediately since the coroutine handler is actually processing the EndOfFrame - stack / queue."
I'm not exactly sure what you mean by that, could you explain a little more?
A coroutine is not a function / method, it's an object. This object has a function which contains your actual code, but the function is ter$$anonymous$$ated at each yield and returns what ever you place behind the yield keyword. WaitForEndOfFrame() will create a new object of WaitForEndOfFrame.
The Unity coroutine scheduler is part of the engine and is responsible for running your coroutine. When you yield the WaitForEndOfFrame object, the scheduler knows that this coroutine should continue at the end of the current frame. When the engine reaches the end of the frame in it's internal loop the scheduler will run all coroutines that has been yielded with WaitForEndOfFrame. Unity stores all this coroutines in a list or queue and will call them one by one. If you yield again with WaitForEndOfFrame it is placed again in this list and will be processed again in the same frame so it has no effect since we are currently at the end of frame.
Here's a great blog about how coroutines work.
Answer by Eric5h5 · Mar 30, 2012 at 02:01 AM
You can't use yield in OnGUI. Like other Update-esque functions, OnGUI runs every frame and can't be interrupted.
Sorry, looks like I needed to reformat my question so it properly separated the code. As you can see, the yield is in it's own function.
Are you calling this function from OnGUI()
?
(If you're not, it'll error out to begin with, so I imagine the answer is yes.)