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 thenachotech1113 · Nov 04, 2014 at 11:35 PM · errorfor-loopindexoutofrangeexception

for loop problem

hello everyone, i`ve been away from unity for a while and im getting a bit of truble with a for loop, it`s supposed to fill an array with the distances at which to spawn objects, here is how it looks like:

     void Update () {
         for (int i = 0; i < 10; i ++){
             distPerSs[i] = -0.25f * i + 500;   //error here
             print("Pos: " + distPerSs[i]);
         }
 

its based in a linear function in which, if i = 0 the value of distPerSs would be 250, and if i = 1000 the distPerSs = 500, yet its giving me an error of "index out of range" in the marked line. any ideas and suggestions are much apreciated

Comment
Add comment · Show 1
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 MrSoad · Nov 04, 2014 at 11:40 PM 0
Share

"index out of range" means that you are trying to access a part of the array that does not exist. If your array is 10 in length, then you can access 0 -> 9 . Check your value for i and make sure that it starts at 0 and that the loop ends when i is the length of the array -1. Use a Debug.Log statement to see what is going on :)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by devluz · Nov 04, 2014 at 11:52 PM

Not sure what your want to do in detail but the error means you try to write or read data from an index that doesn't exist.

e.g. if your array distPerSs has the length of 10 so you create it with this: float[] distPerSs = new float[10]; then you can only use it with index 0 to 9 (distPerSs[0] to distPerSs[9]) but not distPerSs[10].

So the important part is how big is your list? e.g. for the length 10 you could easily do this:

  float[] distPerSs = new float[10];//here you decide the length
  for (int i = 0; i < distPerSs.Length; i ++) //using distPerSs.Length instead of 10
  {
      distPerSs[i] = -0.25f * i + 500;   //error here
      print("Pos: " + distPerSs[i]);
  }

If you do it like this you can change the length easily how you need it without breaking your loop

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

28 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

Related Questions

Insert a semicolon error 1 Answer

x=x Assignment made to same variable 3 Answers

2 Scripting Errors I need help fixing!! 3 Answers

Array index is out of range? 1 Answer

Help solving for loop error 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