Is it possible to create a Box Collider from code?

I don’t mean ‘instantiate’ one, I mean Creating one from code.

I’ve created meshes and it’s colliders from code before, but I don’t know how a box collider works, or if it’s even a Mesh.

Just curious.

Something like this would work:

MeshFilter mf = GetComponent<MeshFilter>();
if (mf && mf.sharedMesh)
{
    Bounds bounds = mf.sharedMesh.bounds;
    BoxCollider collider = mf.gameObject.AddComponent<BoxCollider>();
    collider.center = bounds.center;
    collider.size = bounds.size;
}