- Home /
How to delay a respawn?
Hi, I want to respawn a gameObject (player) but I want it to respawn after a few seconds,I'm using the "yield WaitForSeconds()" to do this, but my problem is that after using it nothing happens, it doesn't even reach my debug.log statement. If I take out the "yield WaitForSeconds()" the player respawns as I want, but when I put it back nothing happens. Heres part of the javascript which is in a gamebject that doesn't ever get destroyed:
var player:GameObject;
function Update(){//this is the position of the undestroyable gameobject
transform.position=Camera.main.ScreenToWorldPoint( Vector3(Screen.width/2, Screen.height/2, 0) );
}
function respawn(){
Debug.Log("hey");
yield WaitForSeconds(1);
Debug.Log("whaaat"); //this debug and the instantiantion doesn't happen
Instantiate(player,Vector3(transform.position.x, transform.position.y,0), Quaternion.identity);
}
I have seen some other questions about how to delay the instantiation but the answers are pretty much the same method I'm using, my question is: why doesn't anything happen after the yield? is there another method to do this?
If I take out the "yield WaitForSeconds()" the player respawns as I want
you just answered it yourself.. keep it out..
Answer by Noob_Vulcan · May 06, 2014 at 04:36 AM
Use
Invoke("respawn",2f);
//this will call respawn fun after 2 sec
function respawn(){
Debug.Log("whaaat"); //this debug and the instantiantion doesn't happen
Instantiate(player,Vector3(transform.position.x, transform.position.y,0), Quaternion.identity);
}
}
call Invoke("respawn",2f)
when your player dies
Thank you! it works fine, I was thinking of doing something else using Time.time, but your answer made it much simpler, thanks again Noob_Vulcan
Your answer
Follow this Question
Related Questions
Delay between each instantiation 3 Answers
Delay in Instantiate only runs once 2 Answers
Inserting a delay 2 Answers
restart level when dead with delay 3 Answers
Coroutine not running after yield return new WaitForSeconds 3 Answers