In the code below at line 25 I get this error:
Failed setting triangles. Some indices are referencing out of bounds vertices.
UnityEngine.Mesh:set_triangles(Int32[ ])
Why do I get this error and how can I fix it?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CreateMesh : MonoBehaviour {
private Mesh mesh;
void Start () {
GenerateMesh ();
}
void GenerateMesh () {
mesh = new Mesh ();
List<Vector3> vertices = new List<Vector3>();
List<int> triangles = new List<int>();
List<Vector2> uv = new List<Vector2>();
GeneratePlane (Vector3.zero, Vector3.up, Vector3.right, vertices, triangles, uv);
GeneratePlane (Vector3.forward, Vector3.up, -Vector3.forward, vertices, triangles, uv);
mesh.vertices = vertices.ToArray();
mesh.triangles = triangles.ToArray();
mesh.uv = uv.ToArray();
mesh.RecalculateBounds();
mesh.RecalculateNormals();
GetComponent<MeshFilter>().mesh = mesh;
}
void GeneratePlane (Vector3 corner, Vector3 up, Vector3 right, List<Vector3> verts, List<int> tris, List<Vector2> uvs) {
verts.Add (corner);
verts.Add (corner + up);
verts.Add (corner + up + right);
verts.Add (corner + right);
int index = tris.Count;
tris.Add(index + 0); tris.Add(index + 1); tris.Add(index + 2);
tris.Add(index + 2); tris.Add(index + 3); tris.Add(index + 0);
uvs.Add(new Vector2(0, 0));
uvs.Add(new Vector2(0, 1));
uvs.Add(new Vector2(1, 1));
uvs.Add(new Vector2(1, 0));
}
}