yield WaitForSeconds(); issue--very specific, apparently

I’ve been looking through other answers for a while now, but I still haven’t been able to find a solution to my problem. Let me show you the code first. It’s very simple:

function GameOver ()
{
	print("GameOver has started");
	yield WaitForSeconds(3);
	print ("Yield waitforseconds in GameOver has ended");
	Application.LoadLevel("screenLose");
}

As it stands, nothing happens after yield WaitForSeconds(); in this code. The console prints “GameOver has started” just fine, but I never see the second print. If I remove the yield WaitForSeconds, everything works just fine.

This function is being called from another script, but like I said, it still works fine up until yield WaitForSeconds();.

Any ideas what’s going wrong here?

You need to start the Coroutine and not simply call it.

So instead of GameOver(), you should write :
StartCoroutine(GameOver());
or
StartCoroutine(“GameOver”);

Is it possible that you have set Time.timescale to 0?

Does it work when you replace the yield with

yield null;

This of course won’t wait 3 seconds, it’s just a test to see if it works.

*** edit ***

Well, the way you call the coroutine is the actual problem. When using UnityScript (Unity’s “Javascript”) you don’t need to call StartCoroutine because the compiler does this automatically for you. The problem is that you use StartCoroutine of this script and not on the one where the coroutine is actually located. So you actually do:

this.StartCoroutine(sceneManager.GetComponent(scriptSceneManager).GameOver());

This will run the coroutine on the current object (the one this script is attached to). This usually isn’t a problem, but since you immediately call

Destroy(gameObject);

after that line, this object is destroyed and all coroutines that run on that object.

In your case you have to use:

var sm = sceneManager.GetComponent(scriptSceneManager);
sm.StartCoroutine(sm.GameOver());

However it’s actually not a good idea to start a coroutine that way. You should make the actual coroutine private and invoke it from a “normal” method. So inside your “scriptSceneManager” class it should look like this:

function GameOver()
{
    GameOverCo();
}

private function GameOverCo ()
{
    print("GameOver has started");
    yield WaitForSeconds(3);
    print ("Yield waitforseconds in GameOver has ended");
    Application.LoadLevel("screenLose");
}

It looks totally silly and looks like it’s pointless to do that extra step, but the compiler actually creates this code:

function GameOver()
{
    this.StartCoroutine(GameOverCo());
}

Now when you call the GameOver method from another object, the coroutine will run here on the “scriptSceneManager” object since we now use StartCoroutine of this object and not the one of the calling object.