'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;

Original meshes Torso replaced

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



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source