- Home /
count the number on enemyes entrering into a trigger
Im having some confusion here, i have a trigger collider and i want if x number of enemies past trought this collider restart the game, and i try this but nothing hapen
#pragma strict
var numEnemies : int = 3;
function OnTriggerEnter2D(other: Collider2D)
{
if(other.CompareTag("Enemy")){
for(var i : int = 0; i > numEnemies; i++){
EndOfLevel();
}
}
}
function EndOfLevel(){
yield WaitForSeconds(2);
// ... and then reload the level.
Application.LoadLevel(Application.loadedLevel);
}
Comment
Why the for loop?
if(other.CompareTag("Enemy"))
{
numEnemies -- ;
if(numEnemies <= 0)
EndOfLevel() ;
}
I tried this with different values for from 0 to and only two enemies could enter the collider before the level restart
if(numEnemies >= 3){ and seems to work but not completely fine
Best Answer
Answer by andrew_196 · Apr 08, 2014 at 12:32 AM
you dont need a loop for that.
function OnTriggerEnter2D(other: Collider2D){
if(other.CompareTag("Enemy")){
numEnemies++;
if(numEnemies >= 3){
EndOfLevel() ;
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613142208im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Only tagged objects raise counter 1 Answer
Adding gameObjects colliding with a trigger to an array 2 Answers
Lap timer using colliders 1 Answer
Inconsistent Trigger events 1 Answer
Use different colliders 3 Answers