- Home /
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[i].GetComponent.<EnemyAI>();
enemyHuntScript = enemyUnit[i].GetComponent.<EnemyHunt>();
}
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
Answer by raulrsd · Mar 15, 2015 at 09:49 PM
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.
Not really sure what you mean. Can you try and explain in more depth?
In your start method I guess you want to store in enemyAIScritpt and enemyHuntScript all the EnemyAI and EnemyHunt components of every existing enemy in the enemyUnit array to enable all of them later. But enemyAIScript and enemyHuntScript only can store one component, because they aren't lists / arrays.
I'm not used to JavaScript, but I'll try to write the code. Hopefully won't be compilation errors :P
You should declare the variables like arrays:
var enemyAIScript : EnemyAI[];
var enemyHuntScript : EnemyHunt[];
Then you Start method should be like this:
function Start(){
enemyUnit = GameObject.FindGameObjectsWithTag("Enemy");
for(var i : int = 0; i < enemyUnit.Length; i++)
{
enemyAIScript[i] = enemyUnit[i].GetComponent.<EnemyAI>();
enemyHuntScript[i] = enemyUnit[i].GetComponent.<EnemyHunt>();
}
alert = false;
caution = false;
normal = true;
}
And finally in the update method, when you want enable/disable the scripts, ins$$anonymous$$d of writing enemyAIScript.enabled = true; you should call these methods this way: EnableAIScripts(true);
function EnableAIScripts(enable : boolean){
for(var i : int = 0; i < enemyAIScript.Length; i++)
{
enemyAIScript[i].enabled = enable;
}
}
function EnableHuntScripts(enable : boolean){
for(var i : int = 0; i < enemyHuntScript.Length; i++)
{
enemyHuntScript[i].enabled = enable;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613190241im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
More Efficient way? 1 Answer
array of boxes 2 Answers
Building an array help 2 Answers
A* Pathfinding AIFollow in JavaScript? 0 Answers
Javascript Array class problem 1 Answer