- Home /
yield WaitForSeconds not returning
I am trying to use WaitForSeconds at the end of a level, but the line after yield WaitForSeconds is never executed. I have tried calling end level with StartCoroutine(endLevel) but that made no difference.
function OnTriggerEnter (hit: Collider) { if (hit.gameObject.tag == "wormProjectile") { Destroy(hit.gameObject); var exp = Instantiate(explosion, gameObject.transform.position, Quaternion.identity); Destroy(gameObject); num_cannon=num_cannon-1; if (num_cannon<=0) { endLevel(); } } }
function endLevel() { TextMsgControl.message = "Level Complete"; WaitForSeconds(5); TextMsgControl.message = "Game Over"; }
Any help appreciated.
Answer by Mike 3 · May 02, 2011 at 05:45 AM
You're destroying the object before the waitforseconds is over, so it'll never finish it
What you probably need to do is start the coroutine on another object which isn't going to die
Just to add to this, disabling the gameobject will also have the same effect.
Your answer
Follow this Question
Related Questions
Why Is yield return new WaitForSeconds() not working 2 Answers
Definition of Script.function() depends on Script.function()... 3 Answers
Understanding yield inside a for or a while loop 2 Answers
About removing the backlight of a button after a set of time 2 Answers
Yield waitforseconds not consistent for animation-unity2D 0 Answers