Enemy health

Hey,
i have a problem with my enemy script. i didn’t found the solution in similar topics.
I have 2 enemies (dublicated). My problem is, that the life of both is decreasing simultaneously.
Do you know why?

1. Enemyscript

public static var Health = 100;

static function Damage(){
	if(Health > 0){
		Health -= Cannon.myDamage;
		Debug.Log(Health);
	}
	
}

2. Cannonscript

var mySpeed : float = 10;
var myRange : float = 10;
public static var myDamage : float = 5;
private var myDist : float;
var counter = 1;

static var testname;

function Update () 
{
	 transform.Translate(Vector3.forward * Time.deltaTime * mySpeed);
	 myDist += Time.deltaTime * mySpeed;
	 if(myDist >= myRange){
		Destroy(gameObject);
		}


}

function OnCollisionEnter(collision: Collision)
{
	if(collision.gameObject.tag == "Enemy")
	{
		if(collision.gameObject.name == "Enemy!"){
		
		
		if(collision.gameObject.GetComponent(CubeEnemy).Health > 0){

					 
		collision.gameObject.GetComponent(CubeEnemy).Damage();}
		
	
	
		if(collision.gameObject.GetComponent(CubeEnemy).Health <= 0){
		
			Destroy (collision.gameObject);	
				
					
		}

Because your health variable is static. There is only ever one instance of a static variablein the scene, and when two or more objects use it, they are always both affected at the same time. Use normal, public variables for your enemies’ health instead. Also rhe static nature of the function might give you problems.

Use static variables/functions only, if they are used uniquely, for example the player’s health.

if they both have the same health variable “health” then both their health is going to go down at the same time.

make 2 different variables

enemyHealthOne
enemyHealthTwo

also within your tag you will have to define which enemy you are hitting.

eg

 if(collision.gameObject.tag == "Enemy")
    {
       if(collision.gameObject.name == "enemyOne"){
enemyHealthOne --;
......

else  if(collision.gameObject.tag == "Enemy")
    {
       if(collision.gameObject.name == "enemyTwo"){
enemyHealthTwo --;
......

Hope this helps

try this

static function Damage(){
    if(this.Health > 0){
       this.Health -= Cannon.myDamage;
       Debug.Log(this.Health);
    }

Thank you very much! I now understand what I did wrong. I works perfectly