- Home /
I keep getting an error on my for loop.
I'm trying to use this code for my for loop to print out the days in the calendar:
void OnGUI()
{
for (i = 0; i < numOfDays + leadDays; i++)
{
if (dates[i].dateOfMonth == 0)
{
labelInput = " ";
GUI.Label(new Rect ( 25, i * 10 ,200,50), labelInput);
}
if (dates[i].dateOfMonth >= 1)
{
labelInput = dates[i].dateOfMonth.ToString();
GUI.Label(new Rect ( 25, i * 10 ,200,50), labelInput);
}
}
}
but I keep getting this error:
ArgumentOutOfRangeException: Argument is out of range. Parameter name: index System.Collections.Generic.List`1[Dates].get_Item (Int32 index) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/List.cs:633) DatesDatabase.OnGUI () (at Assets/SCRIPTS/DatesDatabase.cs:147)
The code actually works fine but I keep getting this error and I'm worried that it might cause some bug later.
Answer by HarshadK · Sep 12, 2014 at 05:31 AM
The error is caused due to your 'numOfDays + leadDays' value exceeds the length of dates.
Try to Debug.Log the values of your 'i' variable when you enter the for loop to check its value against the length of your dates.
I already changed the numofdays + leaddays to dates.count to make it more accurate and debugged the i and I get 31 which is the exact size of the dates list and I still get the error.
I see it now whenever it reaches the end of the loop it repeats itself, all I had to do was dates.count - 1. Thanks man!
array indexes start at zero, so the count should always be the array length less one
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Can't draw GUI.Label text on subpixel values 0 Answers
OnGUI called after LateUpdate screwing up debug text database 1 Answer
GUILayout not working for me C# 1 Answer