ArgumentOutOfRangeException - Comparing two things...

Hello,

So I have a List of Integers and a List of List Objects.

Basically I am just trying to compare the two while in a for loop but for some strange reason I get the error;

“ArgumentOutOfRangeException: Argument is out of range”…

When I debug the two return values I am receiving numbers, so they can’t be null or non-existent ?

public List<List<Object>> listOfLists= new List<List<Object>>();

List<int> intList;

....

if(intList.Count == 0)
{
	intList.Add (new int()); // Tried this and something similar with listOfLists to make sure there is definitely content within them. Debug.Log with a ToString() revealed that they do have values.
	intList[i] = 1;
}

if(listOfLists.Count != intList[i])     // Error points to this..
{

}

So yeah. Please if someone could help me I would be thankful.

You’ll get that exception if i is >= intList.Count

What’s setting i?

i is part of a for loop. When this first iterates it should be set to 0

Not to mention I’m comparing the amount of items in a list, returning a integer, and whatever the int is set at in intList[0]

Sorry if this suggestion doesn’t really work, but have you tried i-1? Seeing only so little doesn’t give me much room to do much gues- I mean, thinking work :slight_smile:

Count is always 1 higher than the key index value in the list.

I’m sure I tried something -1 as I have this error on a few instances on using Count… I’ll give it another go thanks - any other suggestions are welcome though as I am a bit doubtful this’ll work.

Sorry. I got the initial example slightly wrong but I just amended it.

Trying the Count - 1 thing didn’t work. and for some reason the error sometimes indicates this section too…

“if(intList.Count == 0)”

Its such a strange error to me, and the console really doesn’t give much to go on.

Post the full code

If you post your full script it’d be much easier to find the source of the error.

Uuugh. This is really fustrating, sometimes it comes up with this error and sometimes it doesn’t. I mean seriously, how can Debug.Log(intList.Count.ToString()); create an error ??

Post the code!

We are all dieing to help you but you must post relevant code so we can duplicate the issue on our end.

I’m going to take a completely different approach to this if I cannot figure it out tonight. Thanks for the effort but don’t worry about it. I would post my code but its all over the place and the error jumps between sections exactly like what I linked at the start of the post, only it happens in loads of different places across 300 lines of code.

Thanks anyway.

just post the code omg

I will hold myself back… So… I will point here:

Another pair of eyes is very helpful. Another bunch of eyes on the internet in a non-intrusive way is also very helpful

Don’t be afraid of posting “spaghetti code” because we’ve all been there. It’s how you learn to code better.

Ok give me a minute.

OKAY

Thank you for the enthusiasm I appreciate it.

Here … Sorry if there is a rogue curly bracket somewhere. I cut out only the relevant code that I’m working on which is giving me an issue. What I’m basically trying to do is be able to dynamically create a Master windows which hold a int each, changing that int will give GameObject Boxes/windwos that you can drag and drop onto.

So

MasterWindowAmount = 2 would give you 2 entries in ListOfWindows and the ability to set an int which affects each of those entires during the creation as they both refer to i variable on the for loop.

Changing those int variables > 0 will create GameObject inspecter boxes… That’s the idea anyway.

// Code removed because it was wrong and buggy
// Don't want someone finding this page and trying to use it and having the pains I had with it ..
// Current working (ish) version still buggy in some places and very messy so will not post yet.

if I change != to == on line 100 and I uncomment 127 it works…

But only on initialize / startup… if I attempt to change a variable it all goes wrong throwing that error.

Kind of fixed the issue. Still a bit buggy but it mostly works. Will hopefully manage to fully fix it for.

Don’t know quite why it is fixed, just kept tweaking every possible value and variable I could think of. Guess I finally got lucky.

Thanks for the effort anyway, everyone.