C# WaitForSeconds Issue

Need to do a double pause, so asking a coroutine to pause twice. It prints OK, but doesn’t pause. Why?

void Fade(bool showCircle, float waitForFade, float waitBeforeFade)
{
	StartCoroutine(Wait(waitBeforeFade));
	if(showCircle)
	{
		print ("Fading IN");
		if(cameraFadeout)
			cameraFadeout.circleFade("out");
		StartCoroutine(Wait(waitForFade));
	}
	if(showCircle)
	{
		print ("Fading OUT");
		if(cameraFadeout)
			cameraFadeout.circleFade("in");
	}

}

IEnumerator Wait(float waitTimer) 
{
	print ("waiting for " + waitTimer);
	yield return new WaitForSeconds(waitTimer);
}

Oh, to make it more complex, it’s trying to call a JS script:

#pragma strict
var circle_timer : float;

var color_image : Texture2D;
var circle_image : Texture2D;
var speed : float = 5;
var target : Transform;

var public_fader : String;
function Start()
{
	target = gameObject.FindWithTag("Player").transform;
	circleFade("out");
}
function circleFade(fader : String)
{
	if(fader=="out")
	{
		print ("Fading out");
		public_fader="out";
		while(circle_timer<3001)
		{
			circle_timer+=((speed*100)*Time.deltaTime)+circle_timer/50;
			if(public_fader=="in") 
				return;
			yield;
		}	
		circle_timer=3001;
	}
	if(fader=="in")
	{
		print ("Fading in");
		public_fader="in";
		circle_timer=4000;
		while(circle_timer>-1)
		{
			circle_timer-=(((1+speed)*100)*Time.deltaTime)+circle_timer/50;
			if(public_fader=="out") 
				return;
			yield;
		}	
		circle_timer=-1;
	}
}

function OnGUI()
{
GUI.depth=0;
	var player_pos = camera.main.WorldToScreenPoint(target.position);
	if(circle_timer<3000)
	{
	GUI.DrawTexture(Rect(player_pos.x-circle_timer/2-2000,(camera.main.pixelHeight-player_pos.y)-circle_timer/2-4000,4000,4000),color_image);
	GUI.DrawTexture(Rect(player_pos.x-circle_timer/2-4000,(camera.main.pixelHeight-player_pos.y)-circle_timer/2-1000,4000,4000),color_image);
	GUI.DrawTexture(Rect(player_pos.x+circle_timer/2,(camera.main.pixelHeight-player_pos.y)-circle_timer/2-2000,4000,4000),color_image);
	GUI.DrawTexture(Rect(player_pos.x-circle_timer/2,(camera.main.pixelHeight-player_pos.y)+circle_timer/2,4000,4000),color_image);
	GUI.DrawTexture(Rect(player_pos.x-circle_timer/2,(camera.main.pixelHeight-player_pos.y)-circle_timer/2,circle_timer,circle_timer),circle_image);
	}
}

Your problem comes across the list a couple times a week. It stems from a misconception of how coroutines work. When a coroutine hits a ‘yield’ it immediately returns to the calling function and starts executing again, but the line of code after the ‘yield’ will not be executed until after the wait is over. Change your Wait() to:

IEnumerator Wait(float waitTimer) 
{
    print ("waiting for " + waitTimer);
    yield return new WaitForSeconds(waitTimer);
    print ("done waiting");

}

You will see that the ‘done waiting’ does not happen until after the specified wait time. In order to get the wait the way you want it, your fade logic will need to be moved into a coroutine. More info on coroutines:

http://unitygems.com/coroutines/