Problem with respawning enemies

I have a parent with all the enemies attached to it as childs. Every enemy has its own dying script using gameObject.SetActive(false) so I can activate them again with the parent when the player dies.
I have a bool (respawn) in the checkpoint script that is set to true during 0.5 secs when the player dies. This allows me to modify the enemy transform to their initial transform and when the 0.5 secs have passed they can move freely.
Problem is, the respawing script in the parent also uses this bool to set all the children active, but the bool is not always true so after 0.5 secs they turn back to deactivated. I’ve tried making a different bool that is always true but then looping issues occur and the enemies don’t die, so its even more problems. Here’s the Parent script :

`using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemiesRespawnDespawn : MonoBehaviour {

public Transform checkpoint;

void Start () 
{
	for (int a = 0; a < transform.childCount; a++) 
	{
		transform.GetChild (a).gameObject.SetActive (true);
	}
}

void Update () 
{ 
	for (int a = 0; a < transform.childCount; a++) 
	{
		if (checkpoint.GetComponent<chekpoint> ().respawn == true) 
		{
			transform.GetChild (a).gameObject.SetActive (true);
			transform.GetChild (a).GetComponent<HealthComponent> ().CurrentHealth = transform.GetChild (a).GetComponent<HealthComponent> ().MaxHealth;
		} 
	}
}

}`
I’v also tried putting this after the if but it only leads to enemies constantly activating and deactivating.

else if (transform.GetChild (a).GetComponent ().CurrentHealth > 0) { transform.GetChild (a).gameObject.SetActive (true); }

I don’t undestand is why does this happen if nowhere in the script do I say “if respawning is false disable the children”.
The whole script works but the “if (checkpoint.GetComponent ().respawn == true)” part is driving me crazy.
So what should I do? Maybe destroy the enemies instead of deactivating them and then spawing clones? Have I complicated thing too much with this?

@herrik666 Here’s how I would write out the script based on what you’ve shared, please read over it all, I’m touching on a couple things in addition to what you are asking about:

using System.Collections; 
 using System.Collections.Generic; 
 using UnityEngine; 

 public class EnemiesRespawnDespawn : MonoBehaviour 
 { 
     [SerializeField]private chekpoint checkpoint; 
     
     void Start () 
     { 
         for (int a = 0; a < transform.childCount; a++) 
         { 
             transform.GetChild(a).gameObject.SetActive(true); 
         }
     }
 
     void Update ()
     {
         for (int a = 0; a < transform.childCount; a++) 
         { 
             if (checkpoint.respawn)
             { 
                 transform.GetChild(a).gameObject.SetActive(true);
                 
                 HealthComponent healthComponent = transform.GetChild(a).GetComponent<HealthComponent>();
                 healthComponent.CurrentHealth = healthComponent.MaxHealth; 
             }
         }
     }
 }

Beyond this, why are you running this code on an external script anyhow?

You could just put the following onto the chekpoint script (which I assume is attached to each enemy?) p.s. typo.

public class chekpoint : MonoBehaviour 
{
    //...

    private void OnEnable()
    {
        CurrentHealth = MaxHealth;
    }
}

and thus your external script would look as such:

using System.Collections; 
  using System.Collections.Generic; 
  using UnityEngine; 
 
  public class EnemiesRespawnDespawn : MonoBehaviour 
  { 
      [SerializeField]private chekpoint checkpoint; 
      
      void Start () 
      { 
          for (int a = 0; a < transform.childCount; a++) 
          { 
              transform.GetChild(a).gameObject.SetActive(true); 
          }
      }
  
      void Update ()
      {
          for (int a = 0; a < transform.childCount; a++) 
          { 
              if (checkpoint.respawn)
                  transform.GetChild(a).gameObject.SetActive(true);
          }
      }
  }

This way all you have to do is enable the enemies, and they’ll reset their health internally.
You could set CurrentHealth to be a property with a public getter and private setter and same with MaxHealth, if you need to see those values externally, but this would maintain data hiding for security.
Beyond this, as your respawn became more intricate, you could just throw all the resetting into a private method call within Unity’s built in “OnEnable() Method”
or even better yet, if all of your values are set to a default, I believe you can call Reset(); on component references if I am not mistaken. - this would essentially set all the values to that which they would be if you were to just drop the script on a fresh new GameObject; Before tweaking anything - in the editor for example.

And finally, regarding your boolean issues, I would need to really see your chekpoint script, or whatever is attached to the enemies as well, to know exactly - in code - what logic alters the boolean itself.

All in all, a bit more detail is needed, can you edit and share your chekpoint script and any additional relevant scripts for the enemy please. - and as stated by @Casiell Please be sure to drop it in the code snippet tool/option between the quote button and the attachments button. Thank you. - hopefully you get it figured out.

As a side note: Although it seems insignificant, always try your best to see that your naming conventions make sense, in both class naming, and member naming.