Enemy attacks are global??


I have made an attack script. It works very well exept one thing: when the conditions for the attack is meet, the health of the player is removed as many times as there are enemies in the scene. So even if one enemy is close and attacks and one enemy is 10 miles away, the player gets attacked two times. Its like the attacks are global.

here is my code:

using UnityEngine;
using System.Collections;

public class ZombieAttack : MonoBehaviour {
	public float AttackTime;
	public int DamageMax;
	public int DamageMin;
	public float AttackDistance;
	public float DistanceToPlayer;

	private int damage;
	private GameObject player;
	private GameObject zombie;
	private float Xdiff;
	private float Ydiff;
	private bool isAttacking;
	private float attackTimer;

	void Start () {
	isAttacking = false;
	player = GameObject.FindGameObjectWithTag("Player");
	zombie = GameObject.FindGameObjectWithTag("Enemy");

	void Update () {

		Xdiff = Mathf.Abs(player.transform.position.x-zombie.transform.position.x);
		Ydiff = Mathf.Abs(player.transform.position.y-zombie.transform.position.y);
		DistanceToPlayer = Mathf.Sqrt(Mathf.Pow(Xdiff,2)+Mathf.Pow(Ydiff,2));

	if (DistanceToPlayer < AttackDistance && isAttacking == false)
		Attack ();

	if (isAttacking == true)
		Attacking ();

	if (attackTimer > AttackTime)
		Attacked ();
	void Attack () 
	damage = Random.Range (DamageMin,DamageMax);
	PlayerHealth playerHealth = player.GetComponent<PlayerHealth>();
	playerHealth.Health -=damage;
	Debug.Log("Damage given to player: " + damage);
	isAttacking = true;

	void Attacking ()
	attackTimer += Time.deltaTime;

	void Attacked ()
	attackTimer = 0;
	isAttacking = false;

When the DistanceToPlayer requirement is meet on one enemy, every enemy attacks. I only want the enemy that is “close” to attack. Any tips?

Thank you


In Start(), replace:

zombie = GameObject.FindGameObjectWithTag("Enemy");


zombie = this.gameObject;