When i kill one enemy,all enemys die :(

Hi,

i’ve just wrote a few scripts for enemy and plaer health and damage.All is working now,but when i kill one enemy,all enemys die :frowning:

Here are the scipts :

Healthenemy:

static var life = 100;
function Update(){
if(life <= 0){
Destroy(gameObject);
}
}

projectile from gun:

var damage = 5;

function OnTriggerEnter (other : Collider){
Destroy(gameObject);

if (other.gameObject.tag == “Enemy”)

HealthEnemy.life -= damage;

}

Every Enemy is tagged wit “enemy”

Thank you for help

The problem is here:

static var life = 100;

By making the variable static it will be shared between all instances of your class. Try just:

var life = 100;

Since your player health is static id assume you must have the enemy health static as well… and thats the problem. Having the health var set to static means all instances of the enemy are set to that one health var instead of each having their own. Drop the static var in this case.

Now,i got an error for my projectile script:

An instance of type ‘HealthEnemy’ is required to access non static member ‘life’.

That is because you need a reference to an instance of that class. Since that is pretty much just a rewording of what the debugger already told you, I will give you more hands-on hint.

I bet your “HealthEnemy” behaviour is on a GameObject somewhere? If not, make sure that every Enemy GameObject has the Healthenemy behaviour added to them. This can be done using the inspector in Unity Editor for example.

Then, to reduce the health of a particular enemy you must access the behaviour on that particular GameObject. In your code it should probably look something like this:

projectile from gun:

var damage = 5;

function OnTriggerEnter (other : Collider) {
   Destroy(gameObject);

   if (other.gameObject.tag == "Enemy")
      other.gameObject.GetComponent("HealthEnemy").life -= damage;
   }
}

HealthEnemy.life should be

other.GetComponent(“HealthEnemy”).life

You can follow and see tutorials from this user :slight_smile:http://www.youtube.com/watch?v=MQifIzDCvN4

“health System” and others cool tutorials.

WOoaoooaaahh,it’s working now :slight_smile:
Thank you to all of you,you are my new heros :smile: