I am pretty new to understanding meshes, and I’m creating a custom mesh using code, and I’m getting an error I have no idea how to solve, or what it even mean.
“Cleaning the mesh failed”.
What i’m trying to do is to create a mesh with the vertices of an array of raycasts, and put a mesh collider on it to signify an enemies vision.
This is my attempt of an illustration:

And here’s my current code, of course:
public void CreateMeshBetweenPoints(Vector3[] points) {
Mesh mesh = new Mesh();
int[] triangles = new int[points.Length];
int nr = 0;
mesh.vertices = points;
// Create the happy little triangles.
for (int i = 0; i < ((points.Length) / 3); i++) {
triangles.SetValue(0, nr);
nr++;
for(int j = 0; j < 2; j++) {
triangles.SetValue((j+ 1) + i, nr);
nr++;
}
}
for(int k = 0; k < triangles.Length; k++) {
print (triangles[k] + " :: [" + k +"]");
if(k == triangles.Length)
print (triangles[k+1] + " :: [" + k+1 +"] + !!" );
}
mesh.triangles = triangles;
/*
print ("tri: "+ triangles.Length);
print ("vert: "+mesh.vertices.Length);
print ("p: " + points.Length);
*/
detectionCollider.sharedMesh = mesh;
}
Thank you for your attention. Please tell me what I’m doing wrong, or what I should do instead. Also leave a comment if you need more information.
Thanks!
I found out what was wrong, and got it working. The “Cleaning the mesh failed” thing was as pointed out by Cherno (Thank you!) caused by not using the Mesh.Clear() function before setting a mesh, but surprisingly that seems only to be the case with the mesh collider, and this is where I get in to the bigger problem that was present. By using the mesh collider instead of a mesh renderer there were a lot of problems, as instead of wanting a mesh, the mesh collider wanted a shared mesh, which I still don’t know the differences of, by the way. By short term, don’t use a mesh collider like I did, and come up with a different and better solution.
Here’s my current code, which I may mess around with to make more efficient in the future, but apart from that, It’s working fine:
MeshFilter mf;
mf = detectionMeshHolder.GetComponent<MeshFilter> ();
public void CreateMeshBetweenVertices(Vector3[] vertices) {
Mesh mesh = new Mesh ();
int[] triangles = new int[vertices.Length*3]; // The triangles of the mesh.
Vector2[] uvs = new Vector2[vertices.Length]; // The uvs of the mesh.
int nr = 0; // A number to show the iteration number between both for loops.
for (int i = 0; i < vertices.Length -2; i++) {
nr++;
triangles.SetValue(0, nr); // For every 3rd integer of the triangle array, set it to 0.
// Set triangles to 0,1,2,0,2,3,0,3,4,0,4,5... etc.
for(int j = 0; j < 2; j++) {
triangles.SetValue(j+i+1, nr);
nr++;
}
uvs.SetValue(new Vector2(vertices_.x, vertices*.y), i); // Set the UV coordinates.*_
* }*
* // Assign the mesh attributes.*
* mesh.vertices = vertices;*
* mesh.triangles = triangles;*
* mesh.uv = uvs;*
* // Assing the mesh.*
* mf.mesh = mesh;*
* }*
You have a section where ALL Vertices OVERLAP and that is messing up cleaning procedures!!!
Check for this particular case and do not set the collider if neaded