• 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
1
Question by code-blep · Jan 05, 2014 at 11:58 AM · arraylistchecktips

Lists: Avoiding ArgumentOutOfRangeException: Argument is out of range errors

Hi,

I'm trying to expand my knowledge of Lists and I've been experimenting the last few days. My current focus is on avoiding ArgumentOutOfRangeException: Argument is out of range errors when other functions or scripts are accessing a List that is constantly being updated.

Even checking if an element in a List is actually there before doing something can still result in these errors, so I am guessing between checking and doing, the List has changed.

My next focus of attention is here: http://msdn.microsoft.com/en-us/library/3azh197k%28v=vs.110%29.aspx

Am I on the right track or does anyone have some solid advice on how best to avoid out of index scenarios?

Thanks!

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

1 Reply

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

Answer by Traiger · Jan 05, 2014 at 12:30 PM

using ForEach is a good way of avoiding the outofRangeExceptions as you are not responsible for maintaining an item count. For example:-

 var myList = new List<int>();
 
 foreach(var item in myList)
 {
    Debug.Log (item);
 }
Comment
Add comment · Show 5 · 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 code-blep · Jan 05, 2014 at 01:31 PM 0
Share

Hi Traiger,

I have tried that approached and still get errors. For example I have a list that contains a groups of ints that is being modified all the time. The following still gives me out of index errors when the rate of changes gets pretty quick (about every .5 seconds or faster will make it fall over):

 for(var group in intGroups)
 {
     countTotal = group.recordedNumbers.Count;
 }
avatar image code-blep · Jan 05, 2014 at 01:41 PM 0
Share

Actually looking at it closer this seems to work better:

 for(var group in intGroups)
 {
     if(group.recordedNumbers)
     {
         countTotal = group.recordedNumbers.Count;
     }
 }

Will investigate further, and update :)

avatar image Traiger · Jan 05, 2014 at 02:02 PM 0
Share

Are you modifying the list on a timer and then trying to do something with the list in a different thread?

Perhaps this is a threading issue, http://msdn.microsoft.com/en-us/library/system.threading.readerwriterlockslim.aspx may help.

avatar image code-blep · Jan 05, 2014 at 02:45 PM 0
Share

I have various functions in the same script running as While(true) which update the list. It does very much feel like a timing issue of some kind. Will investigate this also and update.

That said the previous code I posted seem to have stopped all errors when reading from a List.

avatar image code-blep · Jan 07, 2014 at 08:36 PM 0
Share

Well it seemed to help so I'll mark this as answered :) Thanks Traiger.

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

19 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

Related Questions

i need a player list help please 0 Answers

A node in a childnode? 1 Answer

unique list of 10 random numbers 4 Answers

How do I check for strings in an array to match up with the index of another array? 1 Answer

Need some help with Array searching logic 1 Answer

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