- Home /
how to respawn an enemy after its health hits 0 and a timer for respawning hits 0?
I've been working on a top down 2d RPG game, So i have an enemy that is a chicken for now and i'm trying to have it respawn after i kill it (health goes to 0) as well as its respawn timer hitting 0. Right now it just keeps spawning a ton of chickens. I have the prefab for the chicken in the scene. as well as this script attached to it for its health/respawning. I left the transform for chicken empty and have the respawn transform set to the prefab for the chicken. If anyone can help that would be greatly appreciated! Thank you!
public int enemyMaxHealth = 100; public int enemyCurHealth = 100;
public float waitToReload;
public float reloadLength;
public Transform chicken;
public Transform respawn;
private bool reload = false;
private playerStats thePlayerStats;
public int expToGive;
// Use this for initialization
void Start () {
enemyCurHealth = enemyMaxHealth;
thePlayerStats = FindObjectOfType<playerStats> ();
chicken = Instantiate (respawn, new Vector3 (0, 0, 0), Quaternion.identity);
chicken.parent = transform;
}
// Update is called once per frame
void Update () {
if (enemyCurHealth <= 0) {
thePlayerStats.addExp (expToGive);
reload = true;
waitToReload = reloadLength;
respawning ();
Destroy (chicken);
}
}
public void respawning(){
if(reload == true){
if(waitToReload <= 0f){
enemyCurHealth = enemyMaxHealth;
chicken = Instantiate(respawn, new Vector3(0,0,0), Quaternion.identity);
chicken.parent = transform;
}
waitToReload -= Time.deltaTime;
}
}
public void HurtEnemy( int damageToGive){
enemyCurHealth -= damageToGive;
}
public void setMaxHealth(){
enemyCurHealth = enemyMaxHealth;
}
} ,
Answer by unidad2pete · Aug 18, 2017 at 09:14 PM
https://docs.unity3d.com/Manual/Coroutines.html
I recommend learn all posible about Coroutines options.
This is an example with your code:
public float waitToReload;
public float reloadLength;
public Transform chicken;
public Transform respawn;
private bool reload = false;
private playerStats thePlayerStats;
public float enemyCurHealth;
public float enemyMaxHealth;
public int expToGive;
// Use this for initialization
void Start()
{
enemyCurHealth = enemyMaxHealth;
thePlayerStats = FindObjectOfType<playerStats>();
chicken = Instantiate(respawn, new Vector3(0, 0, 0), Quaternion.identity);
chicken.parent = transform;
}
// Update is called once per frame
void Update()
{
if (enemyCurHealth <= 0)
{
thePlayerStats.addExp(expToGive);
waitToReload = reloadLength;
StartCoroutine("Timer");
Destroy(chicken);
}
}
public void HurtEnemy(int damageToGive)
{
enemyCurHealth -= damageToGive;
}
public void setMaxHealth()
{
enemyCurHealth = enemyMaxHealth;
}
IEnumerator Timer()
{
bool wait = true; // function start here, set this local bool to true
while(true)
{
if (wait)
{
wait = false; // the first time, the function read this code, set local bool to false, next time, wait mantain false.
yield return new WaitForSecondsRealtime(waitToReload); // here, function wait the specified time on secons
} else
{
// when the specified time on waitToReload is reached, the function continues, in this case is inside of while loop, then, start again on the first line of while loop
// but, before to wait, we was set wait to false, then , this code is readed.
enemyCurHealth = enemyMaxHealth;
chicken = Instantiate(respawn, new Vector3(0, 0, 0), Quaternion.identity);
chicken.parent = transform;
yield break; // Exit of loop Timer , very important
}
}
}
thank you for responding! So i added this bit to my code and made some rearrangements. i put this script on an empty game object and put the health bit on the prefab of the chicken. in the health script i have it calling the respawn code(one above). it now no longer spawns mass amounts of chickens but the coroutine timer doesn't work. I'm new to game coding and unity in general so this concept is still a bit new to me. here is my health script on chicken.
void Update () {
if (enemyCurHealth <= 0) {
thePlayerStats.addExp (expToGive);
//reload = true;
//waitToReload = reloadLength;
//respawn.GetComponent<EnemyRespawn> ().Dead();
Destroy (gameObject);
dead.died ();
}
}
i left other parts of it out. but the "dead.died();" is the part calling the script in the above code to start the timer. only problem is, is that it doesn't count down or respawn anything. Again if you could help on this issue it would be greatly appreciated!
P.S. i comment out sections of code ins$$anonymous$$d of deleting in case i want to go back and try a different technique.
EDIT: i added debug logs and it says it gets to the coroutine timer and that it is in the "Timer" but still nothing happens after. I think it is possibly getting stuck inside of the loops maybe?
Im sorry but I cant know where is the problem, I dont understand how is your objects and script setups, if you share the full code and say me what objects have the scripts components maybe I can find the problem, I think you have problems with object setup in scene.
this is my full code for the objects. my scene is set up as specified above. i have the code for the spawning and with the timer in it attached to an empty game object with my respawn transform set to my chicken prefab. my chicken prefab has the recent code i posted for its health and movements and damage. In the recent code i have it to where once its health reaches 0 to destroy it and call upon(dead.died();) the script for respawning and the timer (which is the script in my original post at the top) as i stated i have debug logs in the timer code you added making sure that it reaches the timer. it does reach the timer thanks to the debug log i added but it does not start the timer (coroutine timer) you showed me. i can "kill" the chicken but the timer never counts down and it never respawns. Hope this clarifies things a bit. really trying to get this respawning script finished i've been stuck on it for a while with no luck other than a horde of chickens spawning and crashing my game lol. Thanks!
Your answer
![](https://koobas.hobune.stream/wayback/20220612133032im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
In My Multiplayer Game only he host Respawns when he dies 0 Answers
How to make a stopwatch that restarts every time the player dies and saves it as a highscore 3 Answers
How to script a death fall and respawn? 0 Answers
kill counter 1 Answer
How do I make my player re-spawn in its original place on collision of enemy? 1 Answer