Player attack deals damage to all of the enemies.

At the moment I am trying to write a script that uses raycast to attack the enemy in front of my player. What happens presently is that the player detects the enemy that is in front of him and deals damage but all enemies with he same script take damage without being near the player. I want the enemy that is in front of the player to be the only one that takes damage.

player script:

public static int playerHealth = 100;
	public int health;
	private Transform playerTrans;
	bool canControl = true;
	bool canAttack = false;
	private Transform MyTarget = null ;
	public int PlayerDamage = 20;
	float Distance;

	
	void Start () 
	{
		playerHealth = 100;
	
	}

	void Update () 
	{
		Debug.DrawRay (transform.position, transform.forward * 5, Color.white);
		health = playerHealth;

		if(playerHealth <= 0)
		{
			canControl = false;

		}

		if(canAttack = true)
		{
			Attacking ();
		}

	}

	 void Attacking ()
	{
		if(Input.GetMouseButtonDown(0))
		{
			RaycastHit hit;
			//Vector3 forward = transform.TransformDirection(Vector3.forward);
			//Vector3 origin = transform.position;

			if(Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit)) 
			{
				if (Distance < 3)
				{
					if(hit.transform.tag == "enemy")
					{

						hit.transform.SendMessage("TakeDamage", 10);

						Debug.Log("I hit the enemy!");
					}

				}
			}
		}
	}

enemy script:

public float maxRange = 5f;
	public float minRange = 5f;
	private Transform target = null;
	float speed = 10;
	private Transform myTransform;
	public float AttackInterval = 3; //1 attack every 3rd second
	private float elapsedTime = 0;
	int attackDamage = 3;
	public static int health;
	public int enHealth;

	void Start () 
	{
		health = 20;


	}
	
	// Update is called once per frame
	void Update () 
	{


		enHealth = health;

		if (target == null) return;
		if(maxRange > 4)
		{
		transform.LookAt(target);
		float distance = Vector3.Distance(transform.position, target.position);
		bool tooClose = distance < minRange;
		Vector3 direction = tooClose ? Vector3.back : Vector3.forward;
		transform.Translate(speed * direction * Time.deltaTime);
		}


			Attack ();

		
	}

	void OnTriggerEnter(Collider other)
	{
		if (other.tag == "Player")
		{
			target = other.transform;

		
		Debug.Log("Made A collision");
		}
	}
	void OnTriggerExit(Collider other)
	{
		if (other.tag == "Player")
		{
			target = null;
			Debug.Log("I can't see you");
		}
	}

	public void Attack()
	{
		if(minRange == maxRange)
		{
		if(Time.time > elapsedTime)
		{
			KnightP.playerHealth -= attackDamage;
			elapsedTime = Time.time + AttackInterval;
			Debug.Log ("I'm attacking the scum");
		}
		}


	}

	public void TakeDamage(int damageAmount)
	{
		health -= damageAmount;

		if(health <= 0)
		{
			DestroyImmediate(this.gameObject);

		}
	}


}

Where did you assign a value to the Distance float in the Player script?
Most probably the bug is around there.
Distance < 3 returns true in more than the required places, I would guess.

@TobiUchiha
You were correct the problem was the distance variable. I most likely left it there when I was previously trying a different method of “attacking”. The script runs fine without it. Thank you so much for the help.