- Home /
The question is answered, right answer was accepted
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 :
isTrigger i set to default = False
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
anyone can help me to fix this ?
Answer by fafase · Apr 09, 2013 at 07:02 PM
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, ins$$anonymous$$d 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");
}
enemy script :
function Update () {
if (healthBarLength <= 0)
{
alive = false;
healthBarLength = 0;
Game$$anonymous$$anager.enemyLeft-=1;
Destroy(gameObject);
}
}
new Game manager (before i don't have)
static var enemyLeft : int;
function Awake(){
enemyLeft = 1;
}
Plane :
function Update()
{
if(Game$$anonymous$$anager.enemyLeft<= 0)
{
Destroy(gameObject);
}
}
function OnCollisionEnter(col:Collision){
if(GameObject.FindGameObjectsWithTag ("Enemy").length > 0)
print("You have not killed them all");
}
when i kill enemy , the Plane still not Destroy . what happen here ?
wow its working now!! thank you~
i have mistake and put 2 Destroy(gameObject) in my Enemy Scripts
Answer by dreammakersgroupAdmin · Apr 09, 2013 at 08:28 PM
do you change isTrigger in somwhere else , if not you code will never been executed and if yes only first "if" will be executed
Answer by LyanApps · Apr 09, 2013 at 07:03 PM
GameObject[] enemy_array;
//http://docs.unity3d.com/Documentation/ScriptReference/GameObject.FindGameObjectsWithTag.html
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;
}
Follow this Question
Related Questions
My vehicle goes through walls and floors disregarding all colliders 2 Answers
Player is falling trough ground.How to fix? 1 Answer
How do I pass through certain colliders but detect all collisions? 1 Answer
Can collision be detected when object IsTrigger? 1 Answer
Problem with colliders when turning isTrigger on / off 3 Answers