- Home /
Enemies respawn in platform game
So I made this really simple android platform game.
Inside each level, there's a checkpoint and, if the player dies after reaching the checkpoint, I want all of the enemies to be brought back to their starting position with full health.
Each enemy has a script, inside this script there's a function that is called every time the enemy or the player dies. This function should bring the enemy back to its starting position with full health.
And here's the problem: for some reason, the script works for some of the enemies and doesn't work for others.
void Reset(){
if (curHealth <= 0) {//if the enemy dies
gameObject.SetActive (false);//deactivate the enemy game object
}
curHealth = maxHealth; //set the enemy's health to maximum
gameObject.transform.position = startPosition; //set the position of the enemy to its starting position
}
void Start()
{
startPosition = gameObject.transform.position; //set the variable "startPosition" to the starting position of the enemy
}
void FixedUpdate()
{
if (curHealth <= 0 || Player.curHealth <= 0){//if the player OR the enemy die
Reset();
}
}
Any ideas on what could I be doing wrong?
EDIT: What happens is that some of the enemies just remain in their position and their health remains the same after the death of the player
How are you finding the objects to call reset on? If you set them to inactive the usual FindByX methods won't pick them up.
Answer by shadowpuppet · Apr 13, 2018 at 06:37 PM
I don't think you need FixedUpdate for that. Update or even LateUpdate. There isn't anything setting the enemy health to ZERO if the player dies ,so there is nothing to reset for the surviving enemies.
if(Player.curHealth <=0){
curHealth = 0;// the enemy health, I presume
Reset();
}
Thanks. I spent so much time on trying to solve this, can't believe it was that easy, thank you again!
glad I could help. If you accept the answer you should mark it so . Just sayin because i was bored today and thought I would try to answer some unanswered questions and came across this again. It also helps people with similar problems see that this post may supply an answer. But again, glad I could help. Pretty new at program$$anonymous$$g so usually my answers are shot down - which is why I try to find those questions no one else has bothered to respond to.
Sure, I'm sorry but I don't really know how Unity Answers works. I also gave you some kind of "point". Have no idea what it is but it looked like a nice thing to do, lol
Your answer
Follow this Question
Related Questions
2D Platformer - How do I have objects that kill the player upon touch/cause respawn? 2 Answers
Save game progress at respawn points 2 Answers
[2D] [C#] Player dying at respawn at checkpoint 1 Answer
Error says object reference not set when it really is? 1 Answer
I'm trying to make a "Game Over" sound when the player dies and respawns 1 Answer