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 *= potentialWaypoint;*
  •   		i++;*
    
  •   	}*
    
  •   }*
    
  • }*

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 declared size of the array inside Getwaypoints and got it working. The issue is the scope of the size of the array.