I need help to attack various enemies with the same tag

Hi I’m trying to get my player attack enemies with the tag “Enemy”, but my code only allows me to attack an enemy. I wanted an attack could reach various of them.
Is there any method of doing this?

PlayerAttack javascript:

	 var target : GameObject[];
	 var attackTimer : float;
     var coolDown : float;
	
	function Start () {
		attackTimer = 0;
    	coolDown = 0.45f;
    	
	
	}
	
    
    function Update() {
     target = GameObject.FindGameObjectsWithTag("Enemy");
        for (var i = 0; i < target.length; i++) {
        
        var ab = target*.gameObject;*

}

  •  if(attackTimer > 0)*
    
  • attackTimer -= Time.deltaTime;*

  • if(attackTimer < 0)*

  • attackTimer = 0;*

  • if(Input.GetButton(“Fire1”)) {*

  • if(attackTimer == 0)*

  • attack ();*

  •  attackTimer = coolDown;*
    
  •  }*
    
  • }*

  • private function attack () {*

  •  var distance : float = Vector3.Distance(target[0].transform.position, transform.position);*
    
  •  var dir : Vector3 = (target[0].transform.position - transform.position).normalized;*
    
  •  var direction : float = Vector3.Dot(dir, transform.forward);*
    
  •  if(distance < 20) {*
    
  •  	if(direction > 0) {*
    
  • var eh : enemyVida = target[0].GetComponent(enemyVida);*

  • eh.addjustCurrentHealth(-10);*

  • }*

  • }*

  • }*

Someone could help me.

In your attack function you have only enemy target[0] processed for attack hence your player attacks only one enemy that is target[0] not others, you can try this

private function attack () {

      for( var i = 0; i < target.length; i++) {
          var distance : float = Vector3.Distance( target*.transform.position, transform.position);*

var dir : Vector3 = (target*.transform.position - transform.position).normalized;*
var direction : float = Vector3.Dot(dir, transform.forward);

if(distance < 20) {
if(direction > 0) {
var eh : enemyVida = target*.GetComponent( enemyVida );*
eh.addjustCurrentHealth(-10);
}
}

}
}
Also you need remove this line target = GameObject.FindGameObjectsWithTag(“Enemy”); from Update() and paste it in Start()