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 ^^’ )

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);
}

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 :slight_smile:

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.