Accessing variables in enemy.

I’m making a tower defense game. The problem I’m having is that whenever a bullet hits, all the enemies lose hp instead of just the one collided with by the bullet. The first mob in each wave is normal, but when it is dead, every mob after him has 0 hp or less and will be killed as soon as a bullet hits them.

Is there a way to access the static variable in the enemy collider without accessing the script. (I’m guessing the problem is that it’s accessing the script and reducing hp by every gameObject that has that type.)

This is the function in bullet script:

var bulletSpeed : int;
var lifeTime : float;
var damage : int;

function Start () {
	lifeTime = Time.time;
}

function Update () {
	transform.Translate(Vector3(0,0,bulletSpeed));
	if (lifeTime + 0.5f < Time.time)
	{
		Destroy(this.gameObject);
		LifeLostScript.enemies--;
	}
}

function OnTriggerEnter (collider : Collider) {
	if (collider.tag == "Enemy")
	{
		collider.GetComponent(EnemyMovementScript).hp -= damage;
		if(collider.GetComponent(EnemyMovementScript).hp <= 0)
		{
			Destroy(collider.gameObject);
		}
		Destroy(this.gameObject);
	}
}

This is the enemyscript:

var speed : int;
var HP : int;
static var hp : int;

function Start () {
	hp = HP;
}

function Update () {
	transform.Translate(Vector3(speed*Time.deltaTime, 0, 0));
}

function OnTriggerEnter (collider : Collider){
    if (collider.tag == "RightRotation") {
        transform.Rotate(0,90,0);
    }
    if (collider.tag == "LeftRotation") {
    	transform.Rotate(0,-90,0);
	}
}

Simple- don’t use static variables! The problem with static variables is that they are not linked to any specific instance- they are global values across every instance of that class. In your case, you probably want to just swap around HP and hp- seeing as you seem to be using one where you should have been using the other. Also, you should set the global one to some number right at the top in the variable designation-

static var HP : int = 100;
var hp : int;

then in Start,

function Start () {
    hp = HP;
}

And all the rest of it remains basically the same.

Remove the static keyword before hp - static vars are unique: even if you have several script instances running at the same time, only one static hp will exist, and everybody will read and write the same variable.