C# List error

List<Rect> Rect_BtnManageItem = new List<Rect>();

...
void OnGUI()
{
   for(n=0;n<=5;n++)
   {
      	Rect_BtnManageItem.Add(new Rect(0,5+n*40,100,35));
	GUI.BeginGroup(Rect_GrpManageItem,"","label");			              
	GUI.Button(Rect_BtnManageItem[n+1],Str_ManageItem[n]); //Str_ManageItem[] is a string Array
	GUI.EndGroup();	
   }
}

while the error:

I dont know why.

any help would be appreciate~

Your array accesses the first 7 elements of your list. Apparently Rect_BtnManageItem does not have 7 elements. You can check how many it does have by printing Rect_BtnManageItem.Count.

Typically, you will also use Rect_BtnManageItem.Count instead of a plain number (like the 5 in your case), to make sure that this error can never happen.

why 7?

from 0 to 5

the total num is 6

n ranges from 0-5, but you access Rect_BtnManageItem like this:

Rect_BtnManageItem[n+1]

Meaning you skip the first element and then try to access the next six: indices 1-6.

yet when I chage the n+1 to n ,the problem is still existed. and the value of Rect_BtnManageItem.Count is indeed 7.
scripts like follows:

public int count;
void OnGUI()
{
	for(int n=0; n<=5; n++)
		{
			if(Rect_BtnManageItem.Count <=5)
			{
				Rect_BtnManageItem.Add(...);
			}
                   count = Rect_BtnManageItem.Count;
		}
}

I dont know why

while I put the Rect_BtnManageItem.Add(…) to Start(){} function,it can work like what I want. Maybe the Cycles of GUI() cause the problem~

my question is that, even though the code run a second time,the value of Rect_BtnManageItem.Count is 6,not 7,I think. anything I missed?

how to use the script in OnGUI() function when I want to set the list value to a list Array with Rect_BtnManageItem.Add(…)

thx~![/code]