Enemy Respawn Bug

When my enimes hit below 0 health I want them to be set inactive for a couple seconds then active again, I can not for the life of me get this to work… I need help (IN C##)
Heres the simple code I have.

	public void respawn() {
		transform.position = respawnhere;
		transform.active = true;
	}

	public void despawn() {
		transform.active = false;
	}

	IEnumerator Example() {
		despawn();
		yield return new WaitForSeconds(35);
		respawn();
	}

I did an edit that changed the script up to work in an easier way after you must have read it. Although I don’t see why it wouldn’t be working for you, guess I’d need to see your scene. Anyway, I thought up an even simpler solution that offers you even more freedom. No parents needed.

Just create an Empty GameObject for your scene, and call it whatever you want, like deathObject or w/e.

Then attach this script: Respawner.cs

using UnityEngine;
using System.Collections;
        
public class Respawner : MonoBehaviour 
{

static public Respawner respawner;
            
	void Awake()
	{
		respawner = this.gameObject.GetComponent<Respawner>(); //declare this script to itself so that the static function can access it
	}
				
	public static void death(GameObject killObj,Vector3 position, float timeToWait)  //this is static, so it can be called from anything else in the game without a GetComponent
	{
		respawner.StartCoroutine(respawner.deathRoutine(killObj,position,timeToWait)); //starting coroutine from within this gameobject.
	}
					
	IEnumerator deathRoutine(GameObject inputObj,Vector3 position, float timeToWait)
	{
		inputObj.SetActive(false);
		yield return new WaitForSeconds(timeToWait);
		inputObj.SetActive(true);
		inputObj.transform.position = position;
	}
}

Now, on your enemies, the ONLY thing you need to do is use:

Respawner.death(this.gameObject,respawnhere,35f);

You can also define the time differently on each enemy if you so choose, by feeding the function the time you want it to wait.

I think the best way to implement what you are talking about while making it reusable for multiple enemies would be:


[Enemy Controller] This will be your parent object, it will control spawning behaviour.

–>[E] [E] …[E] These are the enemies, they all manage their own health and variables.


On the controller you can set up an array with all of your enemies:

public GameObject[] enemy;

then in your update for the controller you can do:

void Update()
{
  foreach(GameObject enem in enemy)
  {
    if((health <= 0)&&( transform.active))
    {
     transform.active = false;
     yield return new WaitForSeconds(35f);
     health = 100;
    }
    else
    {
       transform.active = true;
    }
  }
}

this is a rough way that it can be done.

you could also set eah enemy to register the parent object and send a death message so that it only checks when an enemy dies, that could save a bit of proccessing over checking every update.

I am pretty sure lots of adjestments to my sample may be needed but I hope it helps with the issue.

I see a lot of different suggestions in the comments.

Seeing the original question… I have the idea using enabled in stead of transform.active might be more useful.

So it would be:

    public void respawn() {
       gameObject.transform.position = respawnhere;
       gameObject.enabled = true;
    }
 
    public void despawn() {
       gameObject.enabled = false;
    }
 
    IEnumerator Example() {
       despawn();
       yield return new WaitForSeconds(35);
       respawn();
    }