- Home /
Mesh Collider not work with procedural mesh
I have generate a mesh with code and assign it and appear a box collider.
Destroy(meshCollider);
meshCollider = body.AddComponent<MeshCollider>();
meshCollider.convex = true;
meshCollider.sharedMesh = null;
meshCollider.sharedMesh = meshFilter.sharedMesh;
To be honest I don't know if it helps but you don't have to destroy the MeshCollider if you add a new one anyway. Instead you can do this:
if( !body.TryGetComponent(out meshCollider) ) {
meshCollider = AddComponent<MeshCollider>();
}
Also are you sure that you set the sharedMesh of the mesh filter before you assign the sharedMesh of the mesh collider? That's all ideas I would have here :D Hope it helps
@The-Peaceful I think that he's trying to update the mesh collider to fit around a new mesh.
@sully I understand what he is trying to do here but the way he is doing it (in theory) should work. That's why I asked him
are you sure that you set the sharedMesh of the mesh filter before you assign the sharedMesh of the mesh collider?
the part where he sets the mesh to null and then sets it to the shared mesh should work, as the meshCollider rebuilds the collider's shape if the sharedMesh is set to a new mesh (according to the Scripting API). Meaning it is very much possible that the meshCollider is doing nothing wrong but instead it maybe doesn't get the different mesh?