Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Seventh Stealth · Mar 15, 2015 at 09:21 PM · javascriptarrayai

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Seventh Stealth · Mar 16, 2015 at 09:20 PM 0
Share

Not really sure what you mean. Can you try and explain in more depth?

avatar image raulrsd · Mar 16, 2015 at 10:47 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges