- Home /
Unity5 - "Failed getting triangles"
In my project I'm generating differnet meshes all useing submeshes. Before Unity 5 all the meshes generated without problems. But now it there is a problem wich appear before generating the meshcollider. It says in the console:
"Failed getting triagles. Submesh 1 has no indices. UnityEngine.MeshCollider:set_sharedMesh(Mesh)",
this error applies to every submesh. The mesh itself generates. But the meshcollider won't. Does anyone have an Idea how to fix this? (My english isn't perfect sry ^^' )
Are you passing the right mesh? Did you set the vertices?
Answer by syruf · Jun 17, 2015 at 06:27 AM
I was encountering this error when adding a mesh collider component to a gameobject that had a meshfilter with an empty submesh that i set by mesh.SetTriangles(). One way to prevent an empty submesh would be to omit the submesh entirely, as some of the answers here suggest. That was difficult to handle in my case because I would have to later omit the material from the respective meshrenderer elsewhere in scripts.
Instead, while using mesh.SetTriangles(), i check if the submesh is empty and if so just give it a placeholder triangle with the first vertex (0,0,0). It may cause conflict for some cases but its working okay in my game.
mesh.SetTriangles(subTriangles.ToArray(),subindex);
if(mesh.GetTriangles(subindex).Length < 3){
mesh.SetTriangles(new int[3]{0,0,0},subindex);
}
Answer by bostich83 · Mar 05, 2015 at 12:02 AM
I just had the very same issue today.
Make sure your index count is always > 0.
int subMeshCount = 0;
if(triangles0.Count>0){
mesh.SetTriangles(...)
subMeshCount++;
}
mesh.subMeshCount = subMeshCount;
Hope that helps!
//bostich
//edit:
And don't forget to reduce the material's according to your submeshCount, looks funny otherwise :)
This doesn't work for me. :( This is how my code looks now:
int sub$$anonymous$$eshCount = 0;
if(tris1.Length > 0) {sub$$anonymous$$eshCount++; mesh.SetTriangles(tris1, 0);}
if(tris2.Length > 0) {sub$$anonymous$$eshCount++; mesh.SetTriangles(tris2, 1);}
if(tris3.Length > 0) {sub$$anonymous$$eshCount++; mesh.SetTriangles(tris3, 2);}
if(tris4.Length > 0) {sub$$anonymous$$eshCount++; mesh.SetTriangles(tris4, 3);}
if(tris5.Length > 0) {sub$$anonymous$$eshCount++; mesh.SetTriangles(tris5, 4);}
if(tris6.Length > 0) {sub$$anonymous$$eshCount++; mesh.SetTriangles(tris6, 5);}
if(tris7.Length > 0) {sub$$anonymous$$eshCount++; mesh.SetTriangles(tris7, 6);}
if(tris8.Length > 0) {sub$$anonymous$$eshCount++; mesh.SetTriangles(tris8, 7);}
if(tris9.Length > 0) {sub$$anonymous$$eshCount++; mesh.SetTriangles(tris9, 8);}
mesh.sub$$anonymous$$eshCount = sub$$anonymous$$eshCount;
I think that's because your meshIndex is wrong:
int sub$$anonymous$$eshCount = 0;
int meshIndex = 0;
if(tris.Length > 0) { sub$$anonymous$$eshCount++; mesh.SetTriangles(tri1,meshIndex++)}
That's untested though, because my mesh setup is not as complex as yours ;)
//bostich
I've tried that but it still doesn't work. The problem has to be something different. It's really strange because with the old code it generates the mesh correctly. Only the meshcollider doesn't work.
After doing all that above i do the following:
mesh.Optimize();
collider.shared$$anonymous$$esh = mesh;
but, it should work even without optimize.
$$anonymous$$b you can post more code on pastebin? Really wondering why that is not working for you.
//bostich
This also didn't help. This is the original code I'm using to create the hole mesh. Link
Answer by primemover · Mar 22, 2015 at 05:23 PM
You should paste your code again, that link you have above isn't working.
I was able to fix my issue, and without Optimize(). I have my triangles in an array of integer lists, so I iterate through those to set triangles for the proper material. I just had to have a counter that iterated if that material had any triangles associated with it, if it did I set triangles, but at the same time, I rebuilt my materials array. I do this for each chunk as each chunk would have a different set of materials.
int subMeshCounter = 0;
materialList.Clear();
for (int x = 0; x < trianglesListArray.Length ; x++)
{
if (trianglesListArray[x].Count > 0)
{
chunkMesh.SetTriangles(trianglesListArray[x].ToArray(), subMeshCounter);
materialList.Add(MainGame.maingame.CurrentWorld.Materials[x]);
subMeshCounter++;
}
}
chunkMesh.subMeshCount = subMeshCounter;
GetComponent<Renderer>().materials = materialList.ToArray();
chunkMesh.RecalculateNormals();
chunkMesh.RecalculateBounds();
chunkMeshFilter.mesh = chunkMesh;
chunkMeshCollider.sharedMesh = null;
chunkMeshCollider.sharedMesh = chunkMesh;
Don't know if any of that helps, if not post your code again.
Your answer
Follow this Question
Related Questions
Unity 5 - submeshes don't work as before 1 Answer
Generate Colliders not working on FBX imported object 1 Answer
PrefabUtility does not exist? 1 Answer
Object Refrence Not Set To An Instance Of An Object? 2 Answers
Unity 5.6 upgrade pains 0 Answers