Procedural Mesh Collider Not working

MeshCollider currTriMesh = this.gameObject.AddComponent();
currTriMesh.sharedMesh = null;
currTriMesh.sharedMesh = triangleData.meshFilter.sharedMesh;

I’m trying to add a mesh collider to my procedural mesh and for some reason this is not working. However, if I manually go and change any setting on the mesh collider in the inspector, then go back to the game, it works.


Your code is correct, but probably you’re assigning the mesh to the collider at the wrong time. This explains why it works after you give play and change some settings, and return to the game. You need, after creating the mesh:

mesh.RecalculateBounds (); 

And after that, just assign the mesh, now ready, to the collider:

MeshCollider meshCollider = gameObject.GetComponent <MeshCollider> ();
meshCollider.sharedMesh = mesh;