Time delay enemy respawn
What is the best way to have an enemy respawn after death and after a time delay?
I have tried yield WaitForSeconds (?) and then use the new location to respawn but it respawns instantly. I have use the renderer false but enemy is invisible and still attackes. I can instantiate the enemey prefab but the waypoints and target are missing. I'm new at this but to date have got great assistance from reading forums but now time to get into it.
Thanks in advance
Answer by EION Technology · May 12, 2013 at 12:18 AM
With Unity, the most simple script, the better. Miguel and Uriel have good answers, but the most effective way of spawning an enemy Prefab is by having a Spawn Controller. Ill give you an example in Unity Script (you will need 2 scripts: 1.) SpawnController.js (On a new GameObject) 2.) DeathSignal.js (On enemy GameObject)
//Spawn Controller
var enemy1 : GameObject; //enemy prefab gameobject
var deathTimer = 0; //time since enemy has died.
var spawnDelay = 5; //spawn delay.
static var isDead = "false";
function Update (){
if (isDead == "true"){
deathTimer += 1 * Time.deltaTime; //start death timer.
}
if (isDead == "true" && deathTimer == spawnDelay ){
Spawn();
}
}
function Spawn (){
Instantiate(enemy1, 0,0,0);
deathTimer = 0; //reset death timer after spawning.
}
DeathSignal.js :
// enemyHealth can be attached to the regular health script
// by adding enemyHealth = scriptName.Health; in Update function.
var enemy : GameObject;
static var enemyHealth = 100;
function Update(){
if (enemyHealth <= 0){ //when enemy health is zero or below Signal Death to Spawn Controller
SignalDeath();
}
}
function SignalDeath (){
SpawnController.isDead = "true";
Destroy(enemy);
}
Answer by MiguelDeLaCruz · May 05, 2011 at 08:49 PM
You could write a small manager script that will spawn this prefab and handle the timing and positions of the spawns.
Like Uriel mentioned, you could call GameObject.Destroy() on it. But if you want your game to run smoothly, destroying and spawning enemies could cost a hefty sum of cpu cycles.
One thing you could do is disable gameObject instead. gameObject.active = false. This disables everything on the object. And when you want to respawn the same kind of prefab again, you could reset the object, position it, and gameObject.active = true.
Answer by Uriel_96 · Nov 29, 2010 at 03:23 AM
you have put something simple like this:
Destroy(enemy,1.0);
first you choose the object you want to destroy and then the other the time that he dies.
And you dont want this use the yield WaitForSeconds() but the problem is that you need to check because when you put it at the end(after enemy is death) is different that put it at the begin.
If Enemy is destroyed he is totally gone and cannot be respawned?? is this the case or not. Havn't tried your suggestion as i am still at work will try tonight.
Thanks for the responce
Your answer
Follow this Question
Related Questions
Invoke is not like real time 2 Answers
Oncollisionenter and time,OnCollisonEnter and time. 0 Answers
Hide (not delete) text after given time..? 1 Answer
make enemy cars spawn faster over time! 2 Answers
Mining in RTS 0 Answers