After SkinnedMesh Component's sharedmesh, animator can't find bones at once. i also assign new bones

SkinnedMeshRenderer skinnedMesh;

Animator animator;

SkinnedMeshRenderer protoTypeSkinnedMesh;

Animator protoTypeAnimator;

Transform bones;

GameObject bonesObject;

Transform rootBone;

animator = obj.GetComponent();

skinnedMesh = obj.GetComponent();

protoTypeSkinnedMesh = skinMeshList[id];

skinnedMesh.sharedMesh = protoTypeSkinnedMesh.sharedMesh;

skinnedMesh.sharedMaterial = protoTypeSkinnedMesh.sharedMaterial;

protoTypeAnimator = animators[id];

animator.runtimeAnimatorController = protoTypeAnimator.runtimeAnimatorController;

animator.avatar = protoTypeAnimator.avatar;

Destroy(obj.transform.GetChild(0).gameObject); //hips, for bones

bonesObject = Instantiate(bonesObjs[id]);

bones = bonesObject.transform.GetComponentsInChildren();

bonesObject.transform.SetParent(obj.transform);

rootBone = bonesObject.GetComponent();

skinnedMesh.bones = bones;

skinnedMesh.rootBone = rootBone;

after run this code, the object’s Animator Component show warnings Transform ‘Head’ has been deleted Transform ‘Hand_L’ has been deleted Transform ‘Head_R’ has been deleted …

but these are the bones before they change. i do ‘skinnedMesh.bones = bones;’ And during runtime, if I change something in the Animator component in the inspector window, warning goes away and it works as I intended.

i was try animator.Rebind() but it is still…

unity version is 2020.3.38f1

Not a lot of information to go on here, there could be many things wrong. having said that, it seems like the issue might be related to the order of execution or the initialization of components. You can try to force the update of the Animator component after assigning the new bones by calling animator.Update(0f) after setting the skinnedMesh.bones and skinnedMesh.rootBone. This might help the Animator component to recognize the new bone hierarchy immediately.

Here’s your code with the added animator.Update(0f) call:

SkinnedMeshRenderer skinnedMesh;
Animator animator;
SkinnedMeshRenderer protoTypeSkinnedMesh;
Animator protoTypeAnimator;
Transform[] bones;
GameObject bonesObject;
Transform rootBone;

animator = obj.GetComponent<Animator>();
skinnedMesh = obj.GetComponent<SkinnedMeshRenderer>();

protoTypeSkinnedMesh = skinMeshList[id];
skinnedMesh.sharedMesh = protoTypeSkinnedMesh.sharedMesh;
skinnedMesh.sharedMaterial = protoTypeSkinnedMesh.sharedMaterial;

protoTypeAnimator = animators[id];
animator.runtimeAnimatorController = protoTypeAnimator.runtimeAnimatorController;
animator.avatar = protoTypeAnimator.avatar;

Destroy(obj.transform.GetChild(0).gameObject); //hips, for bones

bonesObject = Instantiate(bonesObjs[id]);
bones = bonesObject.transform.GetComponentsInChildren<Transform>();
bonesObject.transform.SetParent(obj.transform);

rootBone = bonesObject.GetComponent<Transform>();
skinnedMesh.bones = bones;
skinnedMesh.rootBone = rootBone;

animator.Update(0f);

Please try this modification and see if it resolves the warnings and works as expected. Otherwise, please provide more information, these forums are not meant as just code dumps with the expectation that someone will just help. Please respect other peoples time by providing adequate information with which to help.