How to reach multiple GameObjects' value , enemy AI

Hello everyone, I need help for my AI script. I have my enemy AI script almost ready. My enemies have “playerDetected” value. If this value is true they start shooting. So I want this value to be true when player shoots. I did :

function Shoot(){
var target = GameObject.FindWithTag("Enemy");
var scr = target.GetComponent(EnemyAI);
scr.playerDetected=true;
}

this script works perfect only if there is one enemy in the scene. When I put 2 or more enemies to the scene, only one of them starts shooting. Basically what I want to do is, when I shoot all the objects tagged “Enemy” will change their playerDetected value to true in their scripts. Any ideas ? Thanks :).

FindWithTag only returns the first enemy found in the game objects tree. If you want to warn all enemies, use FindGameObjectsWithTag: this function returns an array with all enemies, and you can set them all

function Shoot(){
var targets: GameObject[] = GameObject.FindGameObjectsWithTag("Enemy");
  for (var target in targets){ // warn all enemies in targets[]
    var scr = target.GetComponent(EnemyAI);
    if (scr) scr.playerDetected=true; // if target has the script, set playerDetect
  }
}