Index out of range Exception for Array with 3 values, but no error with 2 values.

Hello: I’m working on animating an a character attack using HOtween; when the player presses the right bumper on an xbox 360 controller a transform will animate though a series of way points. Each way point is part of an array. The code worked fine when the array only had two way points, but for some reason when I add a 3rd way point I get an “Index out of rage exception”. I’m hoping someone can figure out why I would only get this error when I have 3 way points and not for two.

Here is my code with the three way-points that I’m getting the error from:

`public Transform arm;

public Transform swingStart;
public Transform throwReticalPos;
public Transform swingEnd;

public Vector3[] posArray = new Vector3[3];

void Start () {
	HOTween.Init(true, true, true);
}

void Update () {
	posArray[0] = new Vector3(swingStart.position.x, swingStart.position.y, swingStart.position.z);
	posArray[1] = new Vector3(throwReticalPos.position.x, throwReticalPos.position.y, throwReticalPos.position.z);
	posArray[2] = new Vector3(swingEnd.position.x, swingEnd.position.y, swingEnd.position.z);

	if(Input.GetButton("RBumper"))
	{
		Debug.Log("RBumper hit");
	HOTween.To(arm, 1, new TweenParms()

		.Prop("position", new PlugVector3Path(posArray, PathType.Curved))

		);
	}
}`

What’s weird is I don’t get the error when I only have two way-points:

`public Transform arm;

public Transform swingStart;
public Transform throwReticalPos;
public Transform swingEnd;

public Vector3[] posArray = new Vector3[2];

void Start () {
	HOTween.Init(true, true, true);
}

void Update () {
	posArray[0] = new Vector3(swingStart.position.x, swingStart.position.y, swingStart.position.z);
	posArray[1] = new Vector3(throwReticalPos.position.x, throwReticalPos.position.y, throwReticalPos.position.z);
	//posArray[2] = new Vector3(swingEnd.position.x, swingEnd.position.y, swingEnd.position.z);

	if(Input.GetButton("RBumper"))
	{
		Debug.Log("RBumper hit");
	HOTween.To(arm, 1, new TweenParms()

		.Prop("position", new PlugVector3Path(posArray, PathType.Curved))
		);
	}
}`

check this out:

public Transform swingStart;
public Transform throwReticalPos;
public Transform swingEnd;
 
public Vector3[] posArray;
 
void Start () {
    posArray = new Vector3[3];
    HOTween.Init(true, true, true);
}
 
void Update () {
    posArray[0] = new Vector3(swingStart.position.x, swingStart.position.y, swingStart.position.z);
    posArray[1] = new Vector3(throwReticalPos.position.x, throwReticalPos.position.y, throwReticalPos.position.z);
    posArray[2] = new Vector3(swingEnd.position.x, swingEnd.position.y, swingEnd.position.z);
 
    if(Input.GetButton("RBumper"))
    {
       Debug.Log("RBumper hit");
    <a href='http://HOTween.To'>HOTween.To</a>(arm, 1, new TweenParms()
 
       .Prop("position", new PlugVector3Path(posArray, PathType.Curved))
 
       );
    }
}

Public variables get their values from the inspector, not your code. By having:

public Vector3[] posArray = new Vector3[3];

there is no guarantee that there are 3 items in the array.