-2

I have a script that counts how many enemies are left and changes the scene when there are no more enemies. However, it doesn't work, and I can't find the error. I have a really similar script (it's almost the same to be honest) that works perfectly, but I can't figure out how to make this script work.

Here's the code:

public class enemyCountt : MonoBehaviour {

    public static int enemiessCount = 5;

    void Start() {}

    void Update(){
        print("Enemy count is " + enemiessCount ); 

        if(enemiessCount <= 0)
        {
            SceneManager.LoadScene ("bonus");
        }
    }
}

and this is attached to the bullet script:

if (collision.gameObject.tag == "Enemy") {
    enemyCountt.enemiessCount --; 
}
tomy11010
  • 1
  • 1

1 Answers1

0

You are using 2 different scripts, so just enemyCountt.enemiessCount --; wont work. if both scripts are on the same gameobject, then you can use gameObject.enemyCountt.enemiessCount --;

if these arent in the same gameobject, then you can make a reference from the other gameobject with the other script

     public GameObject OtherObject;

     void Update(){
     if (collision.gameObject.tag == "Enemy") {
         OtherObject.enemyCountt.enemiessCount --; 
     }

then you have in the object, where you added the script, a free field, where you can drag & drop your GameObject with the script "enemyCountt" and then it should work, but I'm not quite sure

  • so just enemyCountt.enemiessCount --; wont work Yes it will, they've declared enemiessCount as being static. There are other small issues with your answer as well, the primary one being that it does nothing to address the asker's comments that their Update() method is not running. – Draco18s no longer trusts SE Nov 30 '17 at 19:24