I am duplicating an Animator at runtime, creating a copy of an Animator on a GameObject that has the exact same hierachy.
When I execute the code below it successfully finds the bone Transforms for all of the bones in the ‘Driver’ Animator that is configured in the editor, but every call to DrivenAnimator.GetBoneTransform() returns null. Can anyone help me understand why this is happening and how I can potentially fix this?
I can manually animate the DrivenAnimator using the “Animation” window and the window shows that it finds all of the Transforms related to the Avatar successfully, however I still cannot return them via code.
In the above screenshot ‘autogen_AnimatedSkeleton’ refers to the GameObject with the DrivenAnimator.
// Create duplicate Animator.
DrivenAnimator = animatedGameObject.AddComponent<Animator>();
if (DrivenAnimator != null)
{
// Copy Animator values.
DrivenAnimator.avatar = DriverAnimator.avatar;
DrivenAnimator.speed = DriverAnimator.speed;
DrivenAnimator.applyRootMotion = DriverAnimator.applyRootMotion;
DrivenAnimator.runtimeAnimatorController = Instantiate(DriverAnimator.runtimeAnimatorController);
DrivenAnimator.cullingMode = DriverAnimator.cullingMode;
DrivenAnimator.keepAnimatorControllerStateOnDisable = DriverAnimator.keepAnimatorControllerStateOnDisable;
DrivenAnimator.updateMode = DriverAnimator.updateMode;
DrivenAnimator.enabled = true;
// Rebind the driven animator.
DrivenAnimator.Rebind();
//test code.
for (int i = 0; i < (int)HumanBodyBones.LastBone; ++i)
{
Debug.Log("DRIVER:" + ((HumanBodyBones)i).ToString() + "(" + i + ") | Found? " + (DriverAnimator.GetBoneTransform((HumanBodyBones)i) != null ? "Yes" : "No"));
Debug.Log("DRIVEN:" + ((HumanBodyBones)i).ToString() + "(" + i + ") | Found? " + (DrivenAnimator.GetBoneTransform((HumanBodyBones)i) != null ? "Yes" : "No"));
}
}
The above code is ran in the ‘Awake()’ method of a MonoBehaviour.