How to automatic trigger when complete task

Hi al~
i have a scripts in Plane collider is like this :

function OnTriggerExit( hit : Collider ) 
{
	collider.isTrigger = false;
	
	if(EnemyHealthJava2.alive == true && Health.alive == true && collider.isTrigger == false)
	 {
	 	
	 	collider.isTrigger = false;
	 	print("You Must Kill All enemies");
	 }
	 
	else if(EnemyHealthJava2.alive == false && Health.alive == true && collider.isTrigger == false)
	 {
	 	
	 	collider.isTrigger = true;
	 	Destroy(gameObject);
	 }
	
}

the problems are :

  1. isTrigger i set to default = False

  2. when the player try to cross the collider & enemy Health still alive , the isTrigger will be ‘false’ but when i killed enemies in same time the is Trigger not change to ‘True’ and still ‘False’.

  3. I want Player must kill all enemies and after that can cross the Plane/Box collider

anyone can help me to fix this ?

3 Answers

3

Let’s consider you have a GameManager.js. You need to have a way to keep track of how many enemies you have on scene.
Either you know from the start there is x enemy, then you can have a variable that is set to x on Start

static var enemyLeft:int;

function Start(){
   enemyLeft = 5;
}

then on the enemy script

function Update(){
   if(heath<=0){
      GameManager.enemyLeft-=1; 
      Destroy(gameObject)
   }
} 

Note that only GameManager.enemyLeft-=1; is important as you may have a different logic to kill your enemy.

Finally on your box script:

function Update()
{
   if(GameManager.enemyLeft<= 0)
      Destroy(gameObject);   
}

function OnCollisionEnter(col:Collision){
   if(col.gameObject.CompareTag("Player"))
        print("You have not killed them all");
}

If you were to create enemy during runtime you need to increase the value when instantiating:

function Create(){
   Instantiate(prefab,position,rotation);
   GameManager.enemyLeft +=1;
}

Hi~ why when i put all that code, my Plane/Box collider not yet disappeared when i try to kill. note in here i put 1 enemy for example.

–

If you destroy the enemy when they die, instead of creating a manager you can check to see if there are any enemies left in the OnCollisionEnter(..): function OnCollisionEnter(col:Collision){ if(GameObject.FindGameObjectsWithTag ("Enemy").length > 0) print("You have not killed them all"); }

–

Confuse @.@

–

wow its working now!! thank you~ i have mistake and put 2 Destroy(gameObject) in my Enemy Scripts

–

@LyanApps your solution works as well, but if you have a lot of objects in your scene, get ready for a lag. The function will iterate through the whole hierarchy and you may see your game stopping for half a second. @Apriyanto, since it works for you I close and tick the answer. If not let me know.

–

do you change isTrigger in somwhere else , if not you code will never been executed
and if yes only first “if” will be executed

Already Fixed the problems using new scripts above

–

GameObject enemy_array;
//Unity - Scripting API: GameObject.FindGameObjectsWithTag
void Awake()
{
enemy_array = GameObject.FindGameObjectsWithTag (“Enemy”);
}

//Check the enemies status in Update
void Update(){
 bool alldead = false;
 foreach(GameObject e in enemy_array){
    alldead = alldead || (((Enemy)e).health <= 0);
 }
 if(alldead) collider.isTrigger = true;
}