Search Unity

How to Respawn Enemies once the Player dies?

Discussion in 'Getting Started' started by Rusby, Jan 21, 2015.

  1. Rusby


    Dec 14, 2014
    Hi all,
    Within my current project, I wish for the enemies between the current checkpoint and the next to respawn when the player dies prior to hitting the next checkpoint. I.e. should there be two Checkpoints I and L, when the player is between I and L and he dies, all enemies between I and L should respawn as the player spawns back at I.

    I have attempted to do this using several variables as such:

    When the Player's respawn method is called, a boolean isRespawning is set to true.

    In the update method of the enemy ai script, it checks for whether isRespawning is true, if so it will activate the gameobject (done via another boolean isActive, should this bool not be true then the functions within the class won't be called by the update method, should it be false, it checks for whether isRespawning of the player script is true)

    The main problem I had with this approach is that, I can't for the life of me work out how to change isRespawning to false once all instances of the enemy are activated. Nor can I simply reset the isRespawning bool to false in every instance of the Player Update method as that then prevents any enemies from respawning.

    If you can help me with this problem it would be much appreciated.

    Also, I'm aware that this probably isn't the most efficient / best solution to this problem, so if you have any better ideas please feel free to share them.

    Thanks in Advance.
  2. Senladar


    Jan 1, 2015
    You said that a set of functions in update only get called if isActive is true. Could you not set isRespawning to false there?

    - Enemy isActive, update stuff runs and isRespawning is set to false.
    - Enemy dies, isActive is set to false, updating stuff stops running.
    - Player dies and respawns, isRespawning is set to true, which in turn sets isActive to true which causes isRespawning to be set back to false.
  3. Senladar


    Jan 1, 2015
    Or I guess what you're saying is that you want isRespawning on the PLAYER to be set to false once all the enemies respawn?

    Could you put all of the enemies under an empty gameObject and than do a foreach loop on the empty gameObject to "Activate" all of the children.

    Code (CSharp):
    1. public GameObject enemies;
    3. foreach(Transform child in enemies){
    5. <<do respawn stuff here with each child>>
    7. }
    Once all children are active again, finish the isRespawning code of the player.