# How to change the scene in Unity after all enemies have been killed?

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)
{
}
}
}


and this is attached to the bullet script:

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

• 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? – Vaillancourt Nov 29 '17 at 13:49
• 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 – tomy11010 Nov 29 '17 at 13:51
• Throw in a print("something"); line before SceneManager.LoadScene and tell us if it shows up. That will narrow down the problem – AzulShiva Nov 29 '17 at 13:51
• Maybe a dumb question, but are you sure enemyCountt script is attached to a gameobject in your scene? – Hellium Nov 29 '17 at 14:03
• You might find some use in this earlier question about reacting when all objects of a type have been destroyed – DMGregory Nov 29 '17 at 14:10

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