Submeshes are not visible in scene

Hi, I’m creating a mesh in a script and assign it to a meshfilter. The mesh has a lot of submeshes. For some reason only one submesh is visible in the scene, while all submeshes are visible in the inspector preview. The mesh is created like this:

public Mesh GetMesh()
{
    Mesh m = new Mesh();
    m.subMeshCount = Cells.Count;
    List<Vector3> vertices = new List<Vector3>();

    foreach(var c in Cells)
    {
        vertices.AddRange(c.Mesh.Vertices);
    }
    m.vertices = vertices.ToArray();
    int i = 0;
    int submesh = 0;
    foreach(var c in Cells)
    {
        if(i >= 810)
            Debug.Log("Adding triangles shifted by " + i + "(" + vertices.Count + ")");
        m.SetTriangles(c.Mesh.GetTriangles(i), submesh++);

        i += c.Mesh.Vertices.Count;
    }
    //m.RecalculateNormals();
    //m.RecalculateBounds();
    //m.Optimize();
    return m;
}

I have the same result. Anyone knows the solution?

You need one material for each submesh. So make sure your “renderer.materials” has as many items as you have submeshes. The materials are used in order. So submesh 0 uses material 0 …

Though your mesh creation seem a bit strange. You concat the vertices of several meshes into a large vertex array. However you do not adjust the triangle indices. Since you concat the vertex arrays the first index of the second mesh doesn’t start anymore at “0” but at the offset where the vertices of that mesh start.

“GetTriangles” takes a submesh index. It seems very strange that you use “i” as index which you increment with the vertices count.