- Home /
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?
Answer by _joe_ · Mar 30, 2015 at 10:35 AM
You need to start the Coroutine and not simply call it.
So instead of GameOver(), you should write : StartCoroutine(GameOver()); or StartCoroutine("GameOver");
Tried this. It didn't change anything. :/ Thanks anyway.
If my suggestion didn't work, the problem should be somewhere else. Post the rest of the code so I check it
If you're using JS the compiler will implicitly call the StartCoroutine
for you (docs)[http://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.StartCoroutine.html].
Here, I'll give you the function that calls function GameOver(), because there's a lot of other code; most of it irrelevant:
function PlayerReset ()
{
if (playerVulnerable == 0)
{
if (lives > 1)
{
PlayerInvulnerable ();
Instantiate(explosion, transform.position, explosion.rotation);
transform.position = startPosition;
scene$$anonymous$$anager.GetComponent(scriptScene$$anonymous$$anager).LifeLoss(); //run the "LifeLoss" function in scriptScene$$anonymous$$anager (subtracts a life from the scene$$anonymous$$anager)
lives -= 1;
}
else
{
Instantiate(explosion, transform.position, explosion.rotation);
scene$$anonymous$$anager.GetComponent(scriptScene$$anonymous$$anager).GameOver(); //run the "GameOver" function in scriptScene$$anonymous$$anager
Destroy(gameObject);
}
}
}
Sorry -- the formatting got a little messed up. Thanks for the help.
Basically what i said in my Answer, you are directly calling the coroutine, which doesn't start it, you need to Start it using StartCoroutine.
Here you go :
Replace line 16: scene$$anonymous$$anager.GetComponent(scriptScene$$anonymous$$anager).StartCoroutine("GameOver");
Answer by Bunny83 · Mar 30, 2015 at 11:51 AM
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.
I tried this and had the same problem. So maybe it is a timescale issue, but operations I've used so far that use Time.deltaTime have been working perfectly. Any other ideas?
Yet another case where that so called "simpler syntax" of UnityScript causes unexpected results. UnityScript makes certain phrases of code shorter, but not simpler.