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?

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 can’t use yield in OnGUI. Like other Update-esque functions, OnGUI runs every frame and can’t be interrupted.