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’.
I want Player must kill all enemies and after that can cross the Plane/Box collider
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"); }
@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.
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.
– ApriyantoIf 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"); }
– LyanAppsConfuse @.@
– Apriyantowow its working now!! thank you~ i have mistake and put 2 Destroy(gameObject) in my Enemy Scripts
– Apriyanto@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.
– fafase