- Home /
Need help with AI script
Hello...I need some help with my script, my player can shoot fireballs and can respawn when it touches the enemy, but if i duplicate my enemy and destroy one of them, both of them are destroyed. If anyone knows whats wrong with my script so only one enemy gets destroyed, please help. Heres my script:
#pragma strict
var health = 10;
var TakeDamage : boolean;
function OnTriggerEnter(other : Collider){
if(other.tag == "Player"){
TakeDamage = true;
}
}
function OnTriggerExit(other : Collider){
if(other.tag == "Player"){
TakeDamage = false;
}
}
function Update(){
if(TakeDamage){
if(Input.GetButtonDown("Fire1")){
health --;
}
}
if(health<= 0){
print("Enemy Down");
health = 0;
Destroy(gameObject);
}
}
@portal17 Explain briefly didn't get properly what you are trying to ask??
Can you post the code where you duplicate your enemy as well?
It seems like they are somehow sharing the same values for the health variable or the take damage variable... how are you duplicating the enemies and are they inside of each other or something like that?
you probably checked "Take Damage" on your enemy, make sure it is unchecked to start
Answer by prototype7 · Nov 08, 2012 at 07:56 AM
Try this maybe it works,
#pragma strict
var health = 10;
var TakeDamage : boolean;
var myCollider : Collider;
function OnTriggerEnter(other : Collider){
if(other.tag == "Player"){
TakeDamage = true;
}
myCollider = other;
}
function OnTriggerExit(other : Collider){
if(other.tag == "Player"){
TakeDamage = false;
}
}
function Update(){
if(TakeDamage){
if(Input.GetButtonDown("Fire1")){
health --;
}
}
if(health<= 0){
print("Enemy Down");
health = 0;
if(myCollider != null)
{
Destroy(myCollider.gameObject);
}
}
}
Did you make it mate ? you can change "myCollider" to "enemyHit" to make more sense, so if your fireballs hit/collide the enemy it will recognized the enemy and save it into enemyHit = other in function OnTriggerEnter. and Destroy only that enemy in Destroy(enemyHit.gameObject).
@prototype7 he he You begging to accept answer ???????? :P Not good...........
Thanks.... try to shoot the box with your left mouse click.-->
Answer by Mensley · Nov 20, 2012 at 07:29 AM
hi What did you put the script on enemy or caracter plzz answer as i am having the same trouble
i attached sample package in my comment above, try to explore the Capsule->Camera->Launcher on Hierarchy view
Your answer
Follow this Question
Related Questions
enemy ai going to player 2 Answers
how can i destroy a cube when i press a gui button??? 1 Answer
C# AI, Enemy is flying at me when I jump. 2 Answers
How to make a Tron Light cycle Enemy AI Script ? 0 Answers
Enemy AI help. 0 Answers