Array index is out of range

So, I’m trying to create an automated “Face Creator” for my Mesh, so I can just call a function, to create a face in a specific position, and height/width.

Altho, I’m getting a Out of range, array index error: IndexOutOfRangeException: Array index is out of range.
RoomCreator.CreateFace (Vector3 position, Int32 width, Int32 height) (at Assets/Scripts/BuildingToolsDeveloper/RoomCreator.cs:42)
RoomCreator.CreateMesh () (at Assets/Scripts/BuildingToolsDeveloper/RoomCreator.cs:30)
RoomCreator.CreateRoom () (at Assets/Scripts/BuildingToolsDeveloper/RoomCreator.cs:23)
RoomCreator.Start () (at Assets/Scripts/BuildingToolsDeveloper/RoomCreator.cs:19)

Line 42, is the “CreateFace” function.

Code:

using UnityEngine;
using System.Collections;

public class RoomCreator : MonoBehaviour {

	private Vector3[] Mesh_Vertices;
	private Vector2[] Mesh_Uvs;
	private int[] Mesh_Triangles;

	private MeshFilter meshFilter;
	

	void Start() {
		Mesh_Vertices = new Vector3[100];
		Mesh_Uvs = new Vector2[100];
		Mesh_Triangles = new int[3 * 33];
		meshFilter = GetComponent<MeshFilter>();

		CreateRoom();
	}

	private void CreateRoom() {
		meshFilter.mesh = CreateMesh();
	}

	private Mesh CreateMesh() {
		Mesh m = new Mesh();
		m.name = gameObject.name + "_Mesh";

		CreateFace(new Vector3(0,0,0), 1,1);
		CreateFace(new Vector3(0,1,0), 1,1);

		m.vertices = Mesh_Vertices;
		m.triangles = Mesh_Triangles;
		m.uv = Mesh_Uvs;
		m.RecalculateNormals();

		return m;
	}

	private void CreateFace(Vector3 position,int width, int height) { //Line 41. 42 is under this line.
			Mesh_Vertices[Mesh_Vertices.Length] = new Vector3(position.x, position.y, 0);
			Mesh_Vertices[Mesh_Vertices.Length] = new Vector3(position.x + width, position.y, 0);
			Mesh_Vertices[Mesh_Vertices.Length] = new Vector3(position.x + width, position.y + height, 0);
			Mesh_Vertices[Mesh_Vertices.Length] = new Vector3(position.x, position.y + height, 0);
			
			Mesh_Triangles[Mesh_Triangles.Length] = 0;
			Mesh_Triangles[Mesh_Triangles.Length] = 1;
			Mesh_Triangles[Mesh_Triangles.Length] = 2;
			
			Mesh_Triangles[Mesh_Triangles.Length] = 0;
			Mesh_Triangles[Mesh_Triangles.Length] = 2;
			Mesh_Triangles[Mesh_Triangles.Length] = 3;
			
			Mesh_Uvs[Mesh_Uvs.Length] = new Vector2(0,0);
			Mesh_Uvs[Mesh_Uvs.Length] = new Vector2(0,1);
			Mesh_Uvs[Mesh_Uvs.Length] = new Vector2(1,1);
			Mesh_Uvs[Mesh_Uvs.Length] = new Vector2(1,0);
	}

}

You’re accessing beyond the end of the Mesh_Triangles array, a common mistake. Use Mesh_Triangles.Length-1 to get the last element in the Mesh_Triangles array.

For example:

Mesh_Triangles[Mesh_Triangles.Length-1] = 0

This applies to the other arrays where you’re using array.Length instead of array.Length-1.

Array’s length is always the last index +1. If you try accessing the last element of an array it should be [Length-1] and NOT [Lenght]. For Eg. For an array ‘a’ having 10 elements, a.Length = 10;

a[0] = .. //Length=1
a[1] = .. //Length=2
.
.
a[9] = .. //Length=10
a[10] = null //Out of Range

So if you want last element of the array, It should be

Mesh_Vertices[Mesh_Vertices.Length-1]

For flexible array, ie if you want to add element and don’t care about length of it, use List. For that you just have to use

public List <Vector3> Mesh_Vertices = new List<Vector3>();

private void CreateFace(Vector3 position,int width, int height)
{  
    Mesh_Vertices.Add(new Vector3(position.x, position.y, 0));
    Mesh_Vertices.Add(new Vector3(position.x+width, position.y, 0));
    Mesh_Vertices.Add(new Vector3(position.x+width, position.y+height, 0));
    Mesh_Vertices.Add(new Vector3(position.x, position.y+height, 0));
}