Argument out of range when looping through list

hi,

I’m having an issue with my code where it gives me an error saying argument is out of range and I can’t seem to figure why its giving me that error because strangely enough the still does work as I intended. What I’m trying to do is create a straight line (with positions calculated along it) by placing control points with the mouse at runtime. The issue seems to come from the for loop starting at line 86. Here is the code:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;//Lists are in System.Collections.Generic

public class RuntimeStraight : MonoBehaviour 
{
	// Curve Points
	public Vector3 P1;
	public Vector3 P2;
	// List containing curve points
	public List<Vector3> PointList;
	//public List<Vector3> Nodes;

	// Nodes to instantiate
	public GameObject M1;
	//public Transform M2;
	private Vector3 p0;
	private Vector3 p1;

	// Set Color of Line Renderer
	public Color color = Color.red;
	// Sets Width of Line Renderer
	public float width = 0.2f;

	// Use this for initialization
	void Start () 
	{
		//Initializes Line Renderer component
		LineRenderer lineRenderer = GetComponent<LineRenderer> ();
		if (lineRenderer == null) 
		{
			gameObject.AddComponent<LineRenderer> (); 
		}
		lineRenderer = GetComponent<LineRenderer> ();
		// Makes Line to be rendered in World Space
		lineRenderer.useWorldSpace = true;
		// Sets Line shader
		lineRenderer.material = new Material(Shader.Find("Unlit/Texture"));
	}


	// Update is called once per frame
	void Update ()
	{

		if (Input.GetMouseButtonDown (0)) 
		{

			Ray ray = GetComponent<Camera> ().ScreenPointToRay (Input.mousePosition);
			RaycastHit hit;
			if (Physics.Raycast (ray, out hit, Mathf.Infinity)) 
			{
				Debug.DrawRay (hit.point, hit.normal, Color.red, 5f);
				PointList.Add (hit.point);//Adding the point to array
				GameObject.Instantiate (M1, hit.point, Quaternion.identity);
	
				/*if (PointList.Count == 1) 
				{
					P1 = new Vector3 (PointList [0].x, PointList [0].y, PointList [0].z);
					//Transform.Instantiate (M1, P1, Quaternion.identity);
					//Nodes.Add (P1);
				}	

				if (PointList.Count == 2) 
				{
					P2 = new Vector3 (PointList [1].x, PointList [1].y, PointList [1].z);
					//Transform.Instantiate (M2, P2, Quaternion.identity);
					//Nodes.Add (P2);
				}

				if (PointList.Count == 2) 
				{
					PointList.Clear ();
				}*/
			}
		}

		LineRenderer lineRenderer = GetComponent<LineRenderer> ();
		if (lineRenderer == null || PointList.Count == 1) 
		{
			return; // no points specified
		} 
		Vector3 p0;
		Vector3 p1;
		int numberofPoints;
		for (int j = 0; j < PointList.Count; j++) {
			p0 = PointList [j];
			p1 = PointList [j + 1];

			float dist = Vector3.Distance (PointList [j], PointList [j + 1]);
			int RoundDist = Mathf.RoundToInt (dist);
			numberofPoints = RoundDist;
		


			// update line renderer
			lineRenderer.SetColors (color, color);
			lineRenderer.SetWidth (width, width);
			if (numberofPoints > 0) {
				lineRenderer.SetVertexCount (numberofPoints);
			}

			float t;
			Vector3 position;
			for (int i = 0; i < numberofPoints; i++) {
				t = i / (numberofPoints - 1f);
				position = (1f - t) * p0;
				position += t * p1;
				lineRenderer.SetPosition (i, position);
			}
		}
	}
}

and here is an image of the error.

Hopefully somebody can help figure out whats causing this error. Any help is much appreciated.

Problem is with line 88 as the error report shows: …/RuntimeStraight.cs:88)

The variable j can be equal to the max of the listlength. Yet you look up a index equal to j+1. Which evaluates to listlength+1. Which is impossible.

Use

j < PointList.Count-1

to avoid the out of range error.

Cheers,
Rob