- Home /
When I Destroy 1 Enemy They all Get Destroyed. PLEASE HELP
So Im making a medieval Zombie game, I finished my Enemy AI, it follows the player, it has a character Controller so it wont go through walls or anything, but whenever I kill ONE zombie, ALL the other zombies die too. I think the problem is that the character controller is being destroyed but Im not sure. I need the Controller so the Zombie wont go through objects. Here is my code
enter code here
var speed : float = 5; // the speed
var controller:CharacterController;
static var enemyLife : int = 100; //health
var Explosion : GameObject; //Explosion When Enemy is hit
var Death : GameObject; //explosion when enemy dies
function Start()
{
controller = gameObject.GetComponent(CharacterController);
target = GameObject.FindWithTag("Player").transform; //targets the player
}
function Update()
{
transform.LookAt(target); // the NPC looks at the player
// Here you access the Character Controller component and move your NPC with SimpleMove() giving the speed and the direction(forward).
//As the NPC is looking at you, forward is in your direction.
controller.SimpleMove(speed*transform.forward); // Moves forward
if (enemyLife < 1) //Death
{
Destroy(gameObject); //Destroys enemy
var instance8 = Instantiate(Death,transform.position,transform.rotation); //Explodes
Debug.Log("Enemy was killed");
}
}
//THIS FUNCTION IS FOR WHEN ATTACKS HIT ZOMBIE
function OnCollisionEnter (other : Collision)
{
if(other.gameObject.name == "FireBall(Clone)") //If FireBall hits it
{
enemyLife -=18;
if(Amulet.EAmulet == true) //If Player has a fire amulet
{
Debug.Log("EAmulet is BALLAH");
enemyLife -= 2;
}
Destroy(other.gameObject);
var instance = Instantiate(Explosion,transform.position,transform.rotation);
Debug.Log("Enemy was hit");
}
else if(other.gameObject.name == "PhotonBall(Clone)") //If Photon Ball hits
{
enemyLife -= 20;
if(Amulet.LAmulet == true) //if player has a light amulet
{
Debug.Log("Like a Diamond");
enemyLife -= 1;
}
Destroy(other.gameObject);
var instance2 = Instantiate(Explosion,transform.position,transform.rotation);
}
else if(other.gameObject.name =="Shock(Clone)")
{
enemyLife -= 23;
Destroy(other.gameObject);
var instance3 = Instantiate(Explosion,transform.position,transform.rotation);
}
else if(other.gameObject.name == "FrostBall(Clone)")
{
enemyLife -= 25;
Destroy(other.gameObject);
var instance4 = Instantiate(Explosion,transform.position,transform.rotation);
}
else if(other.gameObject.name == "AquaBall(Clone)")
{
enemyLife -= 34;
Destroy(other.gameObject);
var instance5 = Instantiate(Explosion,transform.position,transform.rotation);
}
else if(other.gameObject.name == "PlasmaBall(Clone)")
{
enemyLife -= 35;
Destroy(other.gameObject);
var instance6 = Instantiate(Explosion,transform.position,transform.rotation);
}
else if(other.gameObject.name == "SpitFire(Clone)")
{
enemyLife -= 16;
if(Amulet.FAmulet == true)
{
Debug.Log("FAmulet Works");
enemyLife -= 2;
}
Destroy(other.gameObject);
var instance7 = Instantiate(Explosion,transform.position,transform.rotation);
}
}
We need to see the code where you call Destory(). Any chance you made the enemies a child of the player?
I see nothing odd here. I think you will be more likely to find your problem nearer a Destroy() call... What code are you using to "kill" your zombies?
Oh sorry I inserted the wrong code on this webpage, I edited my code, so heres the code Ive been using, im not sure why it kills all the zombies
Be careful to post comments as comments not answers! (fixed it for you ;))
As for the question- vinfang is right.
Answer by vinfang · Nov 29, 2013 at 05:12 AM
Looking at the variable you declared, enemyLife, it's static, so that means all instances of your zombies are sharing the same hp variable. So when one of your zombies gets damaged, all your zombies will reflect that damage because they're using the same variable. Make that variable non static and you should be good.
Answer by cameronb_unity287 · Mar 05, 2019 at 03:34 PM
yeah, and the explosion may influence the death because of the explosion whenever the enemy is hit or dies.
Your answer
Follow this Question
Related Questions
Taking damage from an enemy on collision 2 Answers
Enemy Collision Wont Work! PLEASE HELP 0 Answers
Enemy deals damage to player on contact 2 Answers
Enemy death help 1 Answer
Player colliding with animations 0 Answers