- Home /
Respawn timer? Duplicate instance OnDestroy question...
Hi there, new Unity developer here! I am trying to make a sort of "respawn timer" so that my objects will get respawned after a certain amount of time. Due to some strange behavior, the whole setup has became quite a bit more elaborate then I was planning... I currently have an parent object "RespawnHandler" who is alerted whenever a child dies. The child has this method...
void OnDestroy() {
if(shouldTellDestroy) { transform.parent.SendMessage("childDestroyed", gameObject); }
}
...which then calls the parent method...
void childDestroyed(GameObject child) {
Debug.Log("Child Destroyed");
RespawnOverTime.instance.beginTimer(child.gameObject);
}
All goes well, the parent gets the message and prints out "Child Destroyed" but the passed in GameObject ("child") is null. My goal was to use this passed in GameObject later in an Instatiate call.
Any clue how I can work around this? Know a better way to make a respawn timer? Thanks!
EDIT: This code here does the trick, but not if I pass child in to "RespawnOverTime.instance.beginTimer(child)" I have absolutely no clue why...
void childHit(GameObject child) {
Debug.Log("Child Hit");
child.SetActive(false);
StartCoroutine(respawnTimer(child));
}
IEnumerator respawnTimer(GameObject child) {
targetTime = Time.time + 5.0f;
while(true) {
if(Time.time >= targetTime) {
child.SetActive(true);
Debug.Log("Respawned");
}
yield return null;
}
}
Your answer
Follow this Question
Related Questions
Make a simple tree 1 Answer
"Center On Children" programmatically 1 Answer
Distribute terrain in zones 3 Answers
Can one specify the parent of a gameobject in an array? 2 Answers
Access variable in parent script C# 4 Answers