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){
if(other.gameObject.CompareTag("Player")){
other.gameObject.Health -= Health;
Destroy(gameObject);
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.