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!
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.