Hit Detection affects all enemy animations

Hey,

So I have an enemy that Im testing with and I instantiate 2 of them. They are both the same game object so they have the same animations and stuff.

So I coded the enemy animation to play when hit is detected from player:

Enemy Animation.js

var minRunSpeed = 1.0;
private var enemy: Transform;



function Start(){
	enemy = GameObject.FindWithTag ("Player").transform;
	animation.wrapMode = WrapMode.Loop;
	animation["attack"].wrapMode = WrapMode.Once;
	animation["damage"].wrapMode = WrapMode.Once;
	
	animation["idle"].layer = -1;
	animation["walk"].layer = -1;
	animation["damage"].layer = 1;
	
	animation.Stop();
	
}

function Update(){
		var controller : EnemyControls = GetComponent(EnemyControls);
		for (var state : AnimationState in animation) {
			state.speed = 1.5;
				
				if (controller.eMoving == true){
					animation.CrossFade ("walk");
				} else{
					animation.CrossFade ("idle");


				}
				var Hit: PlayerControls = GetComponent(PlayerControls);
				var weapOn : WeaponHit = GetComponent (WeaponHit);
				if (Hit.eHit == true){
					animation.Play ("damage");
					transform.LookAt(enemy);
					//transform.Translate(Vector3.forward * -.15 * Time.deltaTime);
					if (animation["damage"].time >= .2){
						Hit.eHit = false;
						weapOn.weapHit = false;
						//Debug.Log (transform.position);
					}
				}
				
			
			
		}
}

The eHit variable is what is sent and checked after hit is detected from the WeaponHit.js:

static var weapHit: boolean;

function OnTriggerEnter(hit: Collider){
	if(hit.gameObject.tag == "Enemy"){ 
		weapHit = true;	
	}
	
}

and checked in the PlayerControls.js

		var weapOn : WeaponHit = GetComponent (WeaponHit);
		if (weapOn.weapHit == true){
			if (animation["att1"].time >= .4  animation["att1"].time < .6){
				print ("player hit");
				eHit = true;
				Debug.Log(eHit);
			if (eHit == false){
				weapOn.weapHit = false;
			}
			}

			if (animation["att2"].time >= .3  animation["att2"].time < .7){
				print ("player hit");
				eHit = true;
				Debug.Log(eHit);
			if (eHit == false){
				weapOn.weapHit = false;
			}
			}
			if (animation["att3"].time >= .8  animation["att3"].time < 1.04){
				print ("player hit");
				eHit = true;
				Debug.Log(eHit);
			if (eHit == false){
				weapOn.weapHit = false;
			}
			[b]eHit = false[/b];
		}

What basically happens though is that the enemies will play the damage animation if one of them is hit, which is not ideal or what I want. Any work arounds?

static var weapHit: boolean;

Is there a tutorial or example that keeps teaching people to use static variables?

Because they exist across all instances, so if you have ten objects using the same script and set weapHit on one of them, you’ve just set it on all of them. Generally not what you want.

yes, the unity essentials tutorial…

Any fixes since im not supposed to be using that?

how would I access a script that is on another object?

First things first, try removing “static” from the function declaration and see if it runs.

no. nullreference error, ive changed and tried this before multiple times which is why i ended up with static var…

im currently trying to use GameObject.Find("Scythe").GetComponent(WeaponHit).weapHit
but that doesnt work, gets error that weaphit is not a member of UnityComponent…

OK, solved the part of not using static vars using

var scythe = GameObject.Find("Scythe");
		var weapOn : WeaponHit = scythe.GetComponent(WeaponHit);

but the hit detection is still making all enemies animate the damage when I only hit one enemy…