-2
\$\begingroup\$

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 --; 
}
\$\endgroup\$
12
  • 4
    \$\begingroup\$ What do you mean by "it does not work"? What happens instead of what you expect? Is the same script attached to more than one scene? \$\endgroup\$ – Vaillancourt Nov 29 '17 at 13:49
  • \$\begingroup\$ I want to change the scene when there are no more enemies (when enemiessCount <= 0) but it doesn't happen, it just keep going until the time ends and the game is over. And no, it's not the same script attached in more than one scene \$\endgroup\$ – tomy11010 Nov 29 '17 at 13:51
  • 2
    \$\begingroup\$ Throw in a print("something"); line before SceneManager.LoadScene and tell us if it shows up. That will narrow down the problem \$\endgroup\$ – AzulShiva Nov 29 '17 at 13:51
  • 2
    \$\begingroup\$ Maybe a dumb question, but are you sure enemyCountt script is attached to a gameobject in your scene? \$\endgroup\$ – Hellium Nov 29 '17 at 14:03
  • 1
    \$\begingroup\$ You might find some use in this earlier question about reacting when all objects of a type have been destroyed \$\endgroup\$ – DMGregory Nov 29 '17 at 14:10
0
\$\begingroup\$

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

\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$ – Draco18s no longer trusts SE Nov 30 '17 at 19:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.