When I kill one enemy, it kills them all.

Hello, I have been researching this problem for a while and I keep coming across people who need to change their variables to non-static. I’ve tried this and it still isn’t working. I am trying to make a 2D game where a set number of enemies come into the room to try and steal the loot from the player, the player can move towards the enemies and click to make them take damage. Everything is working fine except when I kill one enemy all of them die and they die with one hit. I am using c# as I haven’t used JavaScript very much. Any help is greatly appreciated. :slight_smile:

     public class EnemyScript : MonoBehaviour
     {
     private float curHealth = 40f;
     private float maxHealth = 40f;
     private float decHealth = 4f;
    public static int EnemyCount = 0; 
 
 void Start()
     {
 
         EnemyCount++;
         print("Enemy Count = " + EnemyCount);
 }
     
 void Update()
         {        
     if (curHealth <= 0f)
             {
                 destroyobject();
     
             }
     }
     
     void destroyobject()
         {
             Destroy(this.gameObject);
         }
     
     void OnCollisionStay2D(Collision2D collision){
             if (collision.gameObject.tag == "Player" && Input.GetMouseButtonDown(0))
             {
                 curHealth -= decHealth;
                 LevelManager = GameObject.FindObjectOfType<LevelManager>();
                 LevelManager.EnemyDefeated();
     
             }
 }  

In the LevelManager script function EnemyDefeated() it keeps track of how many enemy objects are left and when all the enemies are gone, it loads a win screen.

Are you sure it kills all the players? Or maybe just start to show the winning screen?
You call the LevelManager.EnemyDefeated method as soon as you kill one enemy, maybe that’s the problem?

I think so, the code for the EnemyDefeated function is below.

    public void EnemyDefeated()
    {
        ChestFind.EnemyCount--;
        print("EnemyCount " + ChestFind.EnemyCount);
        if (ChestFind.EnemyCount <= 0)
            LoadLevel("Win Screen");
    }

I also forgot to add a couple of things to the code above. I have added them now. Thanks for the reply :slight_smile:

Aren’t you missing a curly bracket at the end