• 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 performing 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Find specific element when duplicates exist in list. 1 Answer

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

A node in a childnode? 1 Answer

What type of array should I use to handle a convoy? 0 Answers

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

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges