- Home /
Creating a custom mesh - Cleaning the mesh failed
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!
Try the Clear() function before assigning the generated mesh, as in the example of the Scripting API.
detectionCollider.shared$$anonymous$$esh.Clear();
detectionCollider.shared$$anonymous$$esh = mesh;
It tells me "object reference not set to an instance of an object" refering to the "$$anonymous$$esh.Clear" function. The missing object reference might be because the mesh it is trying to clear does not excist, as it isn't set to anything when it's trying to clear it. I don't see why this would be a problem, though.
In that case, check if the mesh is null.
if( detectionCollider.shared$$anonymous$$esh != null) {
detectionCollider.shared$$anonymous$$esh.Clear();
}
detectionCollider.shared$$anonymous$$esh = mesh;
Double click on the $$anonymous$$esh in the inspector in the $$anonymous$$eshFilter component to see a 3d view of the generated mesh, if it indeed worked. That way, you can at least see if the mesh was assigned, even if it doesn'T show up in the scene view for some reason.
Answer by Stitchey_ · Dec 20, 2015 at 02:26 AM
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[i].x, vertices[i].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;
}
Interesting that you mention that you need to use $$anonymous$$esh.Clear(), but then in the example you don't use it. Why does it work then?
Answer by CMCmaster · Mar 04, 2017 at 04:31 PM
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
A $$anonymous$$eshSection not a continuous line in the vertex array
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
How to place a GameObject with mesh on a mesh "perfectly" through c# script? 1 Answer
How to snap a Prefab to the normal of an irregular mesh 1 Answer
Get forward direction of face hitted by Raycast. 1 Answer
How to Instantiate a prefab on the surface of a curved structure or mesh 1 Answer