How to make a enemy respawn script

Hello, I made a script that makes the enemy respawn after the player kills it. However it just instantly respawns and well, that is kinda lame and stupid for a zombie game. I used yield but it just makes killing the enemy a lot more harder and still doesn’t let the enemy respawn with a delay. Can you please help me with this?

#pragma strict

var Health = 100;
var respawnTransform : Transform;

function ApplyDammage (TheDammage : int)
{
	Health -= TheDammage;
	
	if(Health <= 0)
	{
		RespawnEnemy();
	}
}

function RespawnEnemy()
{
	transform.position = respawnTransform.position;
	transform.rotation = respawnTransform.rotation;
}

You’re just trying to make a delay between death and respawn, right?

Just add yield WaitForSeconds (2); on line 17.

Okay so I have a pretty good implementation, but I’m not really in the mood to code it all up. But i will tell you exactly how I would do this.

Go find a priority queue class you can use online since I don’t think C# has a standard one to use. There’s plenty of implementations out there.
Next, create a RespawnManager script that uses a priority queue to store dead enemy game objects.
Each enemy will have a respawn time. You can use Time.time + respawnTime and store that in a variable called timeToRespawn.
When an enemy dies, it should add itself to the priority queue in the RespawnManager and set itself to inactive. The priority queue should consider enemies with the smallest timeToRespawn as the highest priority.
Then every Update or FixedUpdate, the RespawnManager should check if the enemy at the top of the queue has a smaller timeToRespawn than Time.time. If it is smaller, then pop the enemy off the queue, reset the enemy’s variable’s (i.e. Health), and set the enemy to active. Hopefully you can take it from here.

I don’t do JS but something like this is what you want.

float respawnDelay = 2.0f; //2 seconds
float currentDelay = 0f;

function applyDamage()
{
if (Health <=0)
{
currentDelay = Time.time + respawnDelay;
RespawnEnemy();
}

}

function RespawnEnemy()
{
if(Time.time > currentDelay)
{
//do the instantiate stuff
}
}

Move the enemy off screen when his health goes below 0 and then put him at the reswpawn point after the desired amount of time. I think this should work:

#pragma strict
 
var Health = 100;
var respawnTransform : Transform;
var respawnTimer = 0;
var respawnTime = 3;  
var waitingToRespawn = false;
 
function ApplyDammage (TheDammage : int)
{
    Health -= TheDammage;
 
    if(Health <= 0)
    {
        transform.position = transform.position + Vector3.up*1000; //just need to put him some place off screen
		waitingToRespawn = true;
    }
}
 
function RespawnEnemy()
{
    transform.position = respawnTransform.position;
    transform.rotation = respawnTransform.rotation;
}

function Update()
{
	if(waitingToRespawn)
	{
		respawnTimer += Time.deltaTime;
		if(respawnTimer >= respawnTime)
		{
			RespawnEnemy();
			waitingToRespawn = false;
		}
	}
}