Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by SHG · Dec 12, 2015 at 06:07 PM · array of gameobjectsarray-out-of-range-exceptget component

Accessing same script through array of objects

I'm trying to access an array of gameobjects with the same tag (since that seems faster than findobjectsoftype) with the same script attached and change a bool on the script. The amount of objects changes often, which might be of help to know.

The only error in the console I get is this, which I'm still trying to make sense of. "IndexOutOfRangeException: Array index is out of range. (wrapper stelemref) object:stelemref (object,intptr,object) Car Rewind.Rewind () (at Assets/Custom Assets/Scripts/Car Rewind.js:31) Car Rewind.Update () (at Assets/Custom Assets/Scripts/Car Rewind.js:12)"

and here is the script.

 import UnityStandardAssets.Utility;
 var hasSearched : boolean = false;
 var RotaterScript : UnityStandardAssets.Utility.AutoMoveAndRotate[];
 var rotaters : GameObject[];
 var HubRewindScript : Rewind;
 var i : int;
 
 function Update(){
     if(HubRewindScript.Reverse == true){
         if(!hasSearched){
            Search();
            Rewind();
         } 
     }
     else if(HubRewindScript.Reverse == false){
         if(hasSearched){
             UnRewind();   
             hasSearched = false;
         }
     }
 }
 
 function Search(){
      rotaters = GameObject.FindGameObjectsWithTag("Enemy");
      hasSearched = true;
 }
 
 function Rewind(){
     for(i = 0; i < rotaters.Length; i++){
         RotaterScript[i] = rotaters[i].GetComponent.<UnityStandardAssets.Utility.AutoMoveAndRotate>();
         RotaterScript[i].m_reverse = true;
     }   
 }
 
 function UnRewind(){
     for(i = 0; i < rotaters.Length; i++){
         RotaterScript[i] = rotaters[i].GetComponent.<UnityStandardAssets.Utility.AutoMoveAndRotate>();
         RotaterScript[i].m_reverse = false;
     }   
 }



Comment
Add comment · Show 2
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 fafase · Dec 12, 2015 at 06:15 PM 0
Share

Is the amount of rotaters changing in time?

avatar image SHG fafase · Dec 12, 2015 at 06:25 PM 0
Share

Yes, therefore I cant declare it all at Start().

1 Reply

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

Answer by dhore · Dec 13, 2015 at 02:29 AM

Your "RotaterScript" array is initialized to a length of 0 and then you are trying to access elements of it in the Rewind() and UnRewind() functions. All you need to do to fix this is give it a length, which you can do at the start of the Rewind() function before the for loop - the code would look like this:

 function Rewind(){
     RotaterScript = new UnityStandardAssets.Utility.AutoMoveAndRotate[rotaters.Length];
 
      for(i = 0; i < rotaters.Length; i++){
          RotaterScript[i] = rotaters[i].GetComponent.<UnityStandardAssets.Utility.AutoMoveAndRotate>();
          RotaterScript[i].m_reverse = true;
      }   
  }

Also there is no reason to put this in the UnRewind() function to Get the Components RotaterScript[i] = rotaters[i].GetComponent.<UnityStandardAssets.Utility.AutoMoveAndRotate>(); as you already got the components in the Rewind() function and stored them in a class level variable.

Comment
Add comment · 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

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

30 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Accessing the array of a null item / Moving items in array 0 Answers

Array of materials from array of objects 0 Answers

Referring GameObjet's script from array 2 Answers

Using a list of gameobjects to make GUIbuttons including images. 0 Answers

Getting older elements of an Array/List and looping to the latest elements if index is less than zero 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