When EnemyCount reaches 0, Load Win Screen

Ok so I’m running on some major BULLSHIT.
I’m trying to make that when you kill all enemies, the Win screen loads.
I have 10 enemies in the level, but for some unexplainable reason, that number just WON’T WORK.
I’ve tried 5 enemies or less, it works.
I’ve tried having 10 enemies, but let’s say when EnemyCount == 9, Load the screen, and it works.
It just won’t ever work with 10 enemies and until the counter reaches 0.
My code:

function OnTriggerEnter (other:Collider){ 
 if (name == "Enemy"){ 
 yield WaitForSeconds(.1); 

function Update(){
if(EnemyVar.EnemyCount == 0){

EnemyCount is a variable I set on another script called EnemyVar:

static var EnemyCount : int = 10;

I have no idea what’s going on, could someone help please?


Maybe that the destroy causes some problems in the script execution.

Also, The check on update seems a bit of a waste to me, you may want to set EnemyCount as a property ( https://unity3d.com/learn/tutorials/modules/intermediate/scripting/properties ) and use the set{} method to insert the check.

Maybe this works for you, just place it on your camera or another instance being used once:

You could also try running the check only once every X seconds.

var enemies : GameObject[];
var remainingEnemies : int;

function Update(){
   enemies = GameObject.FindGameObjectsWithTag ("Enemy");
   remainingEnemies = enemies.Length;

   if (remainingEnemies <= 0){