Vector2.MoveTowards with array element, Index was outside the bounds of the array.

I have a simple script attached to an object with a Vector2 array named “points”.
In Update() I try to move the object to the first element of the array (points[0]) using MoveTowards
but I get the error “Index was outside the bounds of the array”.

private Vector2[] points = { new Vector2(11.5f, 7.5f), new Vector2(11.5f, 10.5f) };
private float speed = 5f;
            void Update()
                    transform.position = Vector2.MoveTowards(transform.position, points[0], speed * Time.deltaTime);

If I put:

transform.position = Vector2.MoveTowards(transform.position, new Vector2(11.5f, 7.5f), speed * Time.deltaTime);

it works, but I need to be an array element.

Index out of bounds error means that the index went either too low, or too high of a number than what is in the array. So in your case if you told it to check -1 or 2 or even further than that, you will get that error. As in your array I see 2 indices [0,1].