How to assign bones to mesh in Unity?

I’m developing an avatar customization system. There is one base gameObject with and armature and different meshes for each body part. The script replaces the gameObjects body parts. Now, the problem is that when I try to assign the bones to the new mesh (that is already rigged), it brakes completely.

renderer.rootBone = _bones[0];
Transform[] tmpBones = renderer.bones;
for (int i = 0; i < tmpBones.Length; i++)
{
    for (int j = 0; j < _bones.Length; j++)
    {
        if (tmpBones[i].name == _bones[j].name)
        {
            tmpBones[i] = _bones[j];
        }
    }
}
renderer.bones = tmpBones;

I’m using a fbx for the base meshes and replacing the parts with gltf files with the Siccity gltf utility library.

This is magic for binding bones to a mesh:

NOTE: the above code works but stupidly winds the triangles facing AWAY from the default camera direction so you will see nothing unless you flip it around 180 degrees.