Adding many BoxColliders

I’m trying to add about 30 box colliders at run-time.

Doing this with a for loop and gameObject.AddComponent() results in a noticeable lag-spike of 0.20 seconds.

Is there a way to group this to improve performance? I used to do this in my engine with BepuPhysics by creating the Compound in a separate thread, and dispatching the result back to the game thread to update the physics object. But since unity’s API isn’t thread-safe, I can’t do this. But I also have no idea how to do it differently.

Adding a component is ressource intensive.
That is because the component has to be registered in a global dictionary, and a collider has to be registered in the physics to.

I would recommend you not to use box colliders when you need so many, but instead create a mesh, that covers the used space and use a mesh collider.
You can either generate the mesh by code or make a mesh in your favourite 3D application.

Also try to replace many simillar components with one component, even if the replacement component is more complex.
In general, it costs more performance to iterate through all components than to calculate that one component.

Greetings
Chillersanim