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 Simon 9 · May 31, 2012 at 03:31 AM · arrayindexout of range

"Array index is out of range" for any index value, when array.length>index

Like the title says, I always get an "array index out of range" error, even though the index I'm using is zero (for example) and the array has a length of 4 (or whatever). I'm utterly confounded. This is too simple to not work...

Here is the Code: (error line about 3/4 of the way down)

     var planetsInRouteOrder : GameObject[]; // This oughta' hold 'em.
 planetsInRouteOrder = new GameObject[numPlanets]; // numPlanets will be, for example, 4
 Debug.Log(planetsInRouteOrder.Length); // This checks out just fine.
 var planetsUnityArray = new Array(); // This will hold the ones we're checking
 var anglesUnityArray = new Array(); // Same deal ^ but for angles
 var planetsBuiltinArray : GameObject[]; // Builtin versions to let me reference their values...
 var anglesBuiltinArray : float[];
 
 
 
 for (i=0; i<numPlanets; i+=1) { // Add just the planets we're using this game, to the Arrays
     planetsUnityArray.Add( listOfPlanets[i] );
     anglesUnityArray.Add( anglesToPlanets[i] );// (and add their angles)
 }
 
 var highestSoFar : float;
 var indexOfHighest : int;
 var addedSoFar : int = 0;
 while (planetsUnityArray.length > 0){ // Until the Array is Empty
         
     planetsBuiltinArray = planetsUnityArray.ToBuiltin(GameObject); // Convert to Builtin so we can Reference their Values
     anglesBuiltinArray = anglesUnityArray.ToBuiltin(float);
 
     highestSoFar = -1; // Reset these
     indexOfHighest = -1;
     for (i=0; i<planetsUnityArray.length; i+=1) { // Find Highest Angle Still In Array
         if (anglesBuiltinArray[i] > highestSoFar){ // Update highest
             highestSoFar = anglesBuiltinArray[i];
             indexOfHighest = i;
         }
     }
         // Add to Ordered List and Remove it from both arrays
     //planetsInRouteOrder.Add( planetsBuiltinArray[i] );
     Debug.Log("Added so Far: "+addedSoFar.ToString());
     planetsInRouteOrder[addedSoFar] = planetsBuiltinArray[i]; // ERROR IS HERE - Even a hard coded index causes an out of range error. But using the current index always fails at zero.
     addedSoFar +=1;
     
     planetsUnityArray = new Array(planetsBuiltinArray); // Convert Back so we can Remove.
     anglesUnityArray = new Array(anglesBuiltinArray);
     
     planetsUnityArray.RemoveAt(i);
     anglesUnityArray.RemoveAt(i);
 }
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

2 Replies

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

Answer by simonmc · May 31, 2012 at 03:56 AM

The exception occurs due to the planetsBuiltinArray[i] section of the line in question. the i var is set to the length of the array arfter the previous for loop. Perhaps you meant to do planetsBuiltinArray[indexOfHighest] ?

Comment
Add comment · Show 4 · 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 whydoidoit · May 31, 2012 at 04:00 AM 0
Share

:-) Snap!

avatar image Simon 9 · May 31, 2012 at 05:49 AM 0
Share

Thanks! Duh. lol.

avatar image Simon 9 · May 31, 2012 at 05:59 AM 0
Share

I'm still getting a similar error, with different wording. Now it says the index is -1 or too high (the variable I put in is indexOfHighest). I'm guessing maybe it isn't being set at all. Or something. Too tired. Look at it tomorrow.

avatar image Simon 9 · May 31, 2012 at 03:45 PM 0
Share

Got it working. On to the next thing. :D

avatar image
1

Answer by whydoidoit · May 31, 2012 at 03:59 AM

Surely these lines should be using indexOfHighest not i? i will always be beyond the array range after the for next loop...

   planetsInRouteOrder[addedSoFar] = planetsBuiltinArray[i]; // ERROR IS HERE - Even a hard coded index causes an out of range error. But using the current index always fails at zero.
     addedSoFar +=1;
 
     planetsUnityArray = new Array(planetsBuiltinArray); // Convert Back so we can Remove.
     anglesUnityArray = new Array(anglesBuiltinArray);
Comment
Add comment · Show 1 · 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 Simon 9 · May 31, 2012 at 03:45 PM 0
Share

Thanks! :)

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

6 People are following this question.

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

Related Questions

Array of transform index out of range error 1 Answer

Array index out of range after build 1 Answer

How do I check a List[0], when it's nothing? 1 Answer

Out of range error 1 Answer

Array index is out of range Error, only when array is used to Instantiate 4 Answers


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