Set several meshes to same collider

So my mesh in unity has to many verts so its parted in 2 meshparts.
In my smaller meshes I got the mesh data like:

// Mesh data
private List<Vector3> _verts = new List<Vector3>();
private List<int> _triangles = new List<int>();

MeshFilter[] _filters = testRoom.GetComponentsInChildren<MeshFilter>();

foreach (MeshFilter _filter in _filters)
{
  _verts.AddRange(_filter.mesh.vertices);
  _triangles.AddRange(_filter.mesh.triangles);

  testRoom.AddComponent<MeshCollider>();
  testRoom.GetComponent<MeshCollider>().sharedMesh = _filter.mesh;
}

I would like the mesh collider to span the whole mesh and not just the parts eparately like what it does when I have generate colliders box checked, I cant use this since meshes might be generated at runtime.

i think the problem is that you’re trying to add multiple meshcolliders to the same gameobject, and thus each one overwrites the last one.

1 Like

Aaah I see… How should I add the mesh colliders correctly to the meshparts?

depending on what you want to do either AddComponent them to their own transform (via _filter.transform.AddComponent ()), or create child transfroms to hold them separately.

I would like the mesh collider to span the whole mesh and not just the parts eparately like what it does when I have generate colliders box checked, I cant use this since meshes might be generated at runtime.