- Home /
WaitForSeconds() Not Working
So, this is the code that is not working...
var EnemyObject: GameObject;
var respawnPoint: Transform;
function Dead()
{
Destroy (gameObject);
Respawn();
}
function Respawn()
{
yield WaitForSeconds(5);
var EnemyClone = Instantiate(EnemyObject, respawnPoint.position, transform.rotation);
}
Not sure what I am doing wrong,
anyone able to help?
Thanks in advance!
Answer by robertbu · Nov 16, 2014 at 04:22 AM
You are destroying the game object that has this script on it, so the Respawn() coroutine is also destroyed. You can do it this way:
var EnemyObject: GameObject;
var respawnPoint: Transform;
function Dead()
{
Respawn();
}
function Respawn()
{
renderer.enabled = false;
collider.enabled = false;
yield WaitForSeconds(5);
var EnemyClone = Instantiate(EnemyObject, respawnPoint.position, transform.rotation);
Destroy(gameObject);
}
The idea is to hide/disable this game object until after you Instantiate(). I'm not sure what scripts you have on this game object, but you may have to change what is disabled. Note if this is an EnemyObject, you could just move this object and reenable everything rather than Instantiate() a new one.
Cheers for that man, that worked, however, when the clone is cloned it adds a (clone) after each clone, until it ends like EnemyPrefab(Clone)(Clone)(Clone)(Clone) Im not sure whats going on with that
Your answer
Follow this Question
Related Questions
Make the animation play at the start of the time waited, not the end of it? 1 Answer
Yield WaitForSeconds doesn't work 1 Answer
Respawn on Collision Problems. 2 Answers
JS WaitForSeconds Not Working? 1 Answer
WaitForSeconds wont work for me? 0 Answers