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.
But I’m getting the error: Failed setting triangles. The number of supplied triangle indices must be a multiple of 3.
Which is weird, because I am actually using 6 ints, for the 2 triangles that a face have.
This is my 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() {
firstFace = true;
Mesh_Vertices = new Vector3[100];
Mesh_Uvs = new Vector2[100];
Mesh_Triangles = new int[100];
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);
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) {
Mesh_Vertices[0] = new Vector3(position.x, position.y, 0);
Mesh_Vertices[1] = new Vector3(position.x + width, position.y, 0);
Mesh_Vertices[2] = new Vector3(position.x + width, position.y + height, 0);
Mesh_Vertices[3] = new Vector3(position.x, position.y + height, 0);
Mesh_Triangles[0] = 0;
Mesh_Triangles[1] = 1;
Mesh_Triangles[2] = 2;
Mesh_Triangles[3] = 0;
Mesh_Triangles[4] = 2;
Mesh_Triangles[5] = 3;
Mesh_Uvs[0] = new Vector2(0,0);
Mesh_Uvs[1] = new Vector2(0,1);
Mesh_Uvs[2] = new Vector2(1,1);
Mesh_Uvs[3] = new Vector2(1,0);
}
}