- Home /
The question is answered, right answer was accepted
IndexOutOfRangeException: Array index is out of range.
Hi all, I am trying to add waypoints to the array in C#. But my code is giving me the error. I checked potentialWaypoints[] length 18 as expected. Its showing the error at line indicated in code "IndexOutOfRangeException: Array index is out of range." my code is,
public Transform[] waypoints=new Transform[18];
private int i=0;
void start()
{
GetWaypoints();
}
void GetWaypoints ()
{
Transform[] potentialWaypoints = waypointContainer.GetComponentsInChildren<Transform>();
foreach(Transform potentialWaypoint in potentialWaypoints)
{
if (potentialWaypoint != waypointContainer.transform)
{
/*Error=====>*/ waypoints[i] = potentialWaypoint;
i++;
}
}
}
link the unity process through mono develop and keep a break point at the specified line, now check when it gives null reference(at wat value of i). That would be helpful
Answer by hirenkacha · May 26, 2012 at 07:00 AM
I declared size of the array inside Getwaypoints and got it working. The issue is the scope of the size of the array.
Answer by lyzard · May 24, 2012 at 09:45 AM
maybe you could start by reducing the scope of your index variable i to the GetWayPoints method, it is not safe to let it as a global variable like that, especially if you call GetWaypoints twice.
I am not going to call that method twice. Its being called in Start() and only once.