I pass the end point into GetNextIndex to recognise if its the last point

public class PatrolPath : MonoBehaviour
    {
        const float waypointGizmoRadius = 0.3f;
        private void OnDrawGizmos()
        {
            for (int i = 0; i < transform.childCount; i++)
            {
                int j = GetNextIndex(i, transform.childCount-1);
                //Draw Waypoints
                Gizmos.color = Color.blue;
                Gizmos.DrawSphere(GetWaypoint(i), waypointGizmoRadius);
                Gizmos.color = Color.white;
                Gizmos.DrawLine(GetWaypoint(i), GetWaypoint(j));

            }
        }

        private static int GetNextIndex(int i, int endi)
        {
            if (i < endi) return i + 1 ;
            else return 0;

        }

        private Vector3 GetWaypoint(int i)
        {
            return transform.GetChild(i).position;
        }

Well done.
You can also use the % operator which divides the first number by the 2nd number and returns the remainder of the operation.

private static int GetNextIndex(int i, int endi)
{
    return (i+1) % endi;
}

Privacy & Terms