I have a for loop that is looking at a certain point in a list counting from 82 - 95. For some reason when it gets to 93 it gives me the argument out of range error even though there’s 3 more in the list that the loop refuses to count. Is there something that I’m missing.
You should be using myList.Count
to make sure all your list accesses are in range. Maybe show your code.
Here’s how you can find out!
Steps to success:
- find which collection it is (critical first step!)
- find out why it has fewer items than you expect
- fix whatever logic is making the indexing value exceed the collection
- remember you might have more than one instance of this script in your scene/prefab
Here are some notes on IndexOutOfRangeException and ArgumentOutOfRangeException:
If after all that you are baffled by why you are getting this error, and you are POSITIVE that there are enough items in your collection to satisfy your dereference operation, then you need to put a Debug.Log()
call right before the point where you get the error, and print out two things: the index you are dereferencing, and the Count (or Length) of the collection itself.
Are you removing items from the list from inside the loop?
No, I’m using this code to find them in the points I need but the for loop either keeps stopping short or refuses to acknowledge the last index in the loop. going one extra causes even more errors to pop up.
the problem code is this:
//floor set 1
for (int i = 0; i < 14; i++)
{
Debug.Log( "number point" + i + " " + navCont.LoadedFloors[num].ContainersOnFloor[i]);
westFloorSet1[i].InitalizeSlot(navCont.LoadedFloors[num].ContainersOnFloor[i], this);
}
//floor set 2
for (int i = 14; i < 24; i++)
{
Debug.Log("number point" + i + " " + navCont.LoadedFloors[num].ContainersOnFloor[i]);
Debug.Log("number point" + (i - 14) + " " + navCont.LoadedFloors[num].ContainersOnFloor[i]);
northFloorSet1[i - 14].InitalizeSlot(navCont.LoadedFloors[num].ContainersOnFloor[i], this);
}
//floor set 3
for (int i = 24; i < 34; i++)
{
Debug.Log("number point" + i + " " + navCont.LoadedFloors[num].ContainersOnFloor[i]);
Debug.Log("number point" + (i - 24) + " " + navCont.LoadedFloors[num].ContainersOnFloor[i]);
northFloorSet2[i - 24].InitalizeSlot(navCont.LoadedFloors[num].ContainersOnFloor[i], this);
}
//floor set 4
for (int i = 34; i < 48; i++)
{
Debug.Log("number point" + i + " " + navCont.LoadedFloors[num].ContainersOnFloor[i]);
Debug.Log("number point" + (i - 34) + " " + navCont.LoadedFloors[num].ContainersOnFloor[i]);
eastFloorSet1[i - 34].InitalizeSlot(navCont.LoadedFloors[num].ContainersOnFloor[i], this);
}
//floor set 5
for (int i = 48; i < 62; i++)
{
Debug.Log("number point" + i + " " + navCont.LoadedFloors[num].ContainersOnFloor[i]);
Debug.Log("number point" + (i - 48) + " " + navCont.LoadedFloors[num].ContainersOnFloor[i]);
eastFloorSet2[i - 48].InitalizeSlot(navCont.LoadedFloors[num].ContainersOnFloor[i], this);
}
//floor set 6
for (int i = 62; i < 72; i++)
{
Debug.Log("number point" + i + " " + navCont.LoadedFloors[num].ContainersOnFloor[i]);
Debug.Log("number point" + (i - 62) + " " + navCont.LoadedFloors[num].ContainersOnFloor[i]);
southFloorSet1[i - 62].InitalizeSlot(navCont.LoadedFloors[num].ContainersOnFloor[i], this);
}
//floor set 7
for (int i = 72; i < 82; i++)
{
Debug.Log("number point" + i + " " + navCont.LoadedFloors[num].ContainersOnFloor[i]);
Debug.Log("number point" + (i - 72) + " " + navCont.LoadedFloors[num].ContainersOnFloor[i]);
southFloorSet2[i - 72].InitalizeSlot(navCont.LoadedFloors[num].ContainersOnFloor[i], this);
}
//floor set 8
for (int i = 82; i < 95; i++)
{
Debug.Log("number point" + i + " " + navCont.LoadedFloors[num].ContainersOnFloor[i]);
Debug.Log("number point" + (i - 82) + " " + navCont.LoadedFloors[num].ContainersOnFloor[i]);
northFloorSet1[i - 82].InitalizeSlot(navCont.LoadedFloors[num].ContainersOnFloor[i], this);
}
I have everything filled out and checking with the Debugs the code knows that they are there but for some reason it just stops short when it worked previously.
this code that is a similar error but on a smaller scale:
void applyFloorContainers()
{
print("SettingUpFloors");
for (int i = 0; i < floors.Count; i++)
{
print(floors[i].name);
floors[i].EstablisFloor();
}
}
We know indexing didn’t stop working, so you just have a bug.
I see lots of Debug.Logs() but I don’t see you ever actually printing the actual index used (not an expression, the actual number calculated and stored to a temporary integer variable) and the count / length of the collection.
The longer you resist doing that, the longer you will struggle.
Also, I don’t see you actually having done step 1 above either, identifying what collection is being exceeded.
I solved it for some reason parts of the index turned to null I think it was because of prefabs overriding things when saved