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 jetty112 · Jun 02, 2014 at 06:09 PM · raycastarraylistindex

List gives "Array index is out of range" for no reason

Hi!

How can this possibly give the forementioned error?

 RaycastHit2D[] hit;
 hit = Physics2D.RaycastAll (position, transform.right);
 for(int i = 0; i < hit.Length; i++){
     if(hit[i+1] != null){
         Debug.Log(hit[i+1].collider.name);
         secondTarget = hit[i+1].transform;
         break;
     }
 }

The code compiles, but it give a runtime error and doesnt function correctly.

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 fafase · Jun 02, 2014 at 06:15 PM

First off, nothing happens for no reason in a computer, particularly with such a beginner error.

 if(hit.Length > i){
     Debug.Log(hit[i+1].collider.name);
     secondTarget = hit[i+1].transform;
     break;
 }

So you have an array and you want to perform the action if the i is smaller than hit.Length.

Consider your length to be 10, you list is from 0 to 9. So on the run when i is 9, it is smaller than 10 but you use i + 1 in the method trying to access the index 10 that is out of bound.

See, there is always a reason, even if a bug, the bug has a reason. Computers are dumb.

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 jetty112 · Jun 02, 2014 at 08:08 PM 0
Share

Yea i noticed that myself, i edited the code, to what i meant it to be. What is weird, is that the hit[i+1] is not null but it still gives this error (index is out of range)

Sorry for the wrong code at first.

avatar image Blayer98 · Jun 02, 2014 at 09:05 PM 0
Share

Just read through it again, it might be likely that perfor$$anonymous$$g that action when i is smaller than hit length is the problem.

Also, did you get any script errors when saving? Are you not able to use play mode?

avatar image
0

Answer by rutter · Jun 02, 2014 at 09:08 PM

Classic "off by one" error.

Your loop runs from 0 to Length.

Inside the loop, you access i+1. On the last iteration, i+1 is out of bounds.

You can fix this by checking if i is equal to Length-1 before accessing the array, or by changing your loop so that it runs from 0 to Length-1.

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 Clockmagnet · Jun 26, 2014 at 01:00 PM 0
Share

Usually Length used in cycles without "-1", you just shouldn't check if counter <= Length. $$anonymous$$aybe I'm wrong, but i consider "Length-1" as bad tone in this case. We can just put i < length.

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

23 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

Related Questions

How do I find a local variables index in a foreach loop? 1 Answer

Array index is out of range and Raycast question 0 Answers

For all elements above a certain index 1 Answer

How do I return the index of an array of sprites as an int? 1 Answer

Find specific element when duplicates exist in list. 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