- Home /
Failed setting triangles in my mesh
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);
}
}
Answer by Eric5h5 · Jan 15, 2014 at 03:21 AM
You have Mesh_Triangles = new int[100];
. 100 is not a multiple of 3. If you're using 6 triangle indices then the triangles array should be of size 6.
I should really have realized that. I don't know why I didn't. Either way, it fixed it! $$anonymous$$uch appreciated mate.
Don't know if you are still there, but the problem resumes, now the Array index is out of range.
IndexOutOfRangeException: Array index is out of range. RoomCreator.CreateFace (Vector3 position, Int32 width, Int32 height) (at Assets/Scripts/BuildingToolsDeveloper/RoomCreator.cs:42) RoomCreator.Create$$anonymous$$esh () (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)
You're referring to an element in an array, where the array isn't big enough. The code you have here is presumably not the code you're using now, since the line numbers don't match up, so I can't say anything more.
Your answer
Follow this Question
Related Questions
Array index is out of range 2 Answers
Mesh creation by code not working? 0 Answers
Multiple Cars not working 1 Answer
Create a Triangle Mesh. 0 Answers
Mesh adaption 1 Answer