Hi,
im currently working on a respawn-script, which I can use for several GameObjects. The player deactivates the object via trigger, after that, the respawn-script on the object should wait a few seconds, then activate itself again:
void OnDisable()
{
StartCoroutine (WaitForSpawn ());
}
public IEnumerator WaitForSpawn(){
yield return new WaitForSeconds (respawnTime);
gameObject.active = true;
}
The problem is, that a deactive object can’t use a coroutine. Is there another way to do that?
Greetings
J.
Hi.
You can disable the renderer components or other behaviour-controller scripts.
This is how you can replace it with deactivation.
Or you can use a external coroutine at an always active object.
Thanks.
The answer is yes, but not on that object. An easier and cleaner way is to make another object manage the respawns, even if it is just one.
Do that somewhere else. Something like this
On the Object to respawn
void OnDisable(){
manager.Died(this);
}
On the manager side
Edit: Thanks to methos5K for pointing out the typos
public void Died(MyObjectScript target)
{
StartCoroutine (WaitForSpawn (target));
}
public IEnumerator WaitForSpawn(MyObjectScript target)
{
yield return new WaitForSeconds (target.respawnTime);
target.gameObject.active = true;
}
Typo there, I’m sure you meant ‘public void Died(MyObjectScript target)’
1 Like
You forgot to make it public, but you’re welcome
Haha thanks, owe you two now. Not my day today