Specific enemy variable affects all enemies

All the enemies in my game have health and on collision with the player, they subtract their current level of health from the players.

The problem is that if the player reduces one of their health to zero, all of them disappear. At the same time, if I set their health to private variable, the health won't sutbract from the enemies health on collision.

public var Health: int = 100;

function OnCollisionEnter(other: Collision){
other.gameObject.Health -= Health;
rigidbody.velocity = -rigidbody.velocity; 

I'm not sure if this is exactly what you're looking for, but it should at least give you an idea as to how to go about modifying variables in other scripts without using static vars. If this script is attached to an enemy object any time it comes into contact with the player it will subtract it's current health from the player then self destruct.


var enemyHealth : int;

function OnCollisionEnter(col : Collision)
        var tempScript = col.gameObject.GetComponent("name of script that handles health");
        tempScript.health -= enemyHealth;

In Java, their are things called getter/setter methods, which are private, to access private variables...

So, try making it private, and then add in a setter method, that brings in the damage, and then subtract it and set it there.

Make sure it's not static too...

Um, can't think of anything else right now, but you can try this =).