Updating Mesh Collider causes lag

I created a plane that the player can modify, but updating the mesh collider causes a significant drop in frames. I use this code to update collider:

hitTransform.GetComponent<MeshCollider>().sharedMesh = hitTransform.GetComponent<MeshFilter>().sharedMesh;

Is there any way to optimize it?

you could save a reference to the mesh/collider components, could help but i don’t think it this can be the problem in your case.

MeshCollider collider;
MeshFilter filter;
void start() {
collider = hitTransform.Getcomponent<MeshCollider>();
filter = hitTransform.Getcomponent<MeshFilter>();
// your line of code:
collider.sharedMesh = filter.sharedMesh;

this is the only thing i can see right away you could improve, not sure if changing a meshCollider has any special effects on framerate… did u try use the profiler to figure out what might cause this?

Ive done pooling tests with colliders before and other than wheel colliders exponential performance hits were taken when removing a group of 2d colliders or adding a group of ‘normal’ colliders

u might want to disable the collider before you make changes and enable it again when you are done (using a temporary copy maybe?)

What really helps is the brand new Unity 2019.3 bake offloading capability: