Noonb array question - Boolean makes a script activate for all elements in an array. Problem is the only element that gets the script activated is the first one.

var alert : boolean;
var caution : boolean;
var normal : boolean;
var alertTimer : float;
var cautionTimer : float;
var alertTime : float = 30;
var cautionTime : float = 30;
//CHASING
var statusGUI : UI.Text;
var guardSpawn : Transform;
var enemyUnit : GameObject;
var enemyAIScript : EnemyAI;
var enemyHuntScript : EnemyHunt;

 function Start(){
 enemyUnit = GameObject.FindGameObjectsWithTag("Enemy");
  for(var i : int = 0; i < enemyUnit.Length; i++)
    {
 enemyAIScript = enemyUnit*.GetComponent.<EnemyAI>();*

enemyHuntScript = enemyUnit*.GetComponent.();*
}

alert = false;
caution = false;
normal = true;

}
function Update(){

if(alert == true){
if(alertTimer >= 0){
print(“alert”);
statusGUI.text = "ALERT : "+alertTimer;
alertTimer -= Time.deltaTime;
enemyHuntScript.enabled = true;
enemyAIScript.enabled = false;
}else{
//ALERT FINISHED
//SWITCH TO CAUTION
ActivateCaution();
print(“CAUTION”);
statusGUI.text = “”;
}
}
//CAUTION
if(caution == true){
if(cautionTimer >= 0){
print(“CAUTION”);
statusGUI.text = "CAUTION : "+cautionTimer;
cautionTimer -= Time.deltaTime;
}else{
//
alert = false;
caution = false;
normal = true;
statusGUI.guiText.text = “”;
}
}
//NORMAL
if(normal == true){
alert = false;
caution = false;
print(“Normal”);
alertTimer = 30;
cautionTimer = 30;
statusGUI.text = “NORMAL”;
enemyAIScript.enabled = true;
enemyHuntScript.enabled = false;
}
}

Sorry if the title is mucked up. It’s kinda hard to explain my problem. Basically I’m making a stealth game. All the enemies have three states controlled by a manager. The manager the script above attached to it. On start up it finds all the gameObjects with the tag enemy. It sorts through them in the for loop and enemyAIScript and enemyHuntScript are assigned to each enemyUnit index. enemyAIScript is the script used for enemies while in the normal and caution phase and enemyHuntScript is the script used for enemies while in the alert phase.
When in alert I want the enemyHuntScript activated for all enemies in the enemyUnit array - And vice versa with enemyAIScript and normal.
Heres the problem though. When Alert is activated only one of the enemyUnits has the enemyHuntScript activated. The others need to have theirs activated manually through the editor. And its the same story when in normal phase. Only the first element has the enemyAIScript activated and the others need theirs activated through editor.
I’m a programming noob and I wont shy away from the fact that I probably messed up somewhere in the for loops. Anyone know how to fix it so all the elements get the script activated not just element 1?
Thanks in advance
Stealth

The problem is that enemyAIScript and enemyHuntScript aren’t a list. In Start(), when you finish the loop where you assign value to this variables, they are being assigned with the component of the last enemy in enemyList, because you are overwriting it every time.

You should declare enemyAIScript and enemyHuntScript as a List or an array. Then in your loop you should add every enemy’s component to the proper list and finally to enable them, you’ll need to do another loop and enable every member of the list.

I would write the code but I’m not familiarized with javascript.