Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question Not understanding why Animator.GetBoneTransform() returns null on Animator created at runtme.

Discussion in 'Scripting' started by Crazycarpet, Sep 23, 2022.

  1. Crazycarpet

    Crazycarpet

    Joined:
    Dec 5, 2015
    Posts:
    47
    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.*

    Code (CSharp):
    1. // Create duplicate Animator.
    2. DrivenAnimator = animatedGameObject.AddComponent<Animator>();
    3. if (DrivenAnimator != null)
    4. {
    5.     // Copy Animator values.
    6.     DrivenAnimator.avatar = DriverAnimator.avatar;
    7.     DrivenAnimator.speed = DriverAnimator.speed;
    8.     DrivenAnimator.applyRootMotion = DriverAnimator.applyRootMotion;
    9.     DrivenAnimator.runtimeAnimatorController = Instantiate(DriverAnimator.runtimeAnimatorController);
    10.     DrivenAnimator.cullingMode = DriverAnimator.cullingMode;
    11.     DrivenAnimator.keepAnimatorControllerStateOnDisable = DriverAnimator.keepAnimatorControllerStateOnDisable;
    12.     DrivenAnimator.updateMode = DriverAnimator.updateMode;
    13.     DrivenAnimator.enabled = true;
    14.  
    15.     // Rebind the driven animator.
    16.     DrivenAnimator.Rebind();
    17.  
    18.     //test code.
    19.     for (int i = 0; i < (int)HumanBodyBones.LastBone; ++i)
    20.     {
    21.         Debug.Log("DRIVER:" + ((HumanBodyBones)i).ToString() + "(" + i + ") | Found? " + (DriverAnimator.GetBoneTransform((HumanBodyBones)i) != null ? "Yes" : "No"));
    22.         Debug.Log("DRIVEN:" + ((HumanBodyBones)i).ToString() + "(" + i + ") | Found? " + (DrivenAnimator.GetBoneTransform((HumanBodyBones)i) != null ? "Yes" : "No"));
    23.     }
    24. }
    25.  
    The above code is ran in the 'Awake()' method of a MonoBehaviour.
     
    Last edited: Sep 23, 2022
  2. Crazycarpet

    Crazycarpet

    Joined:
    Dec 5, 2015
    Posts:
    47
    Solved, I had not made a SkinnedMeshRenderer on the driven Animator and that led to there being no bones.
     
    Kurt-Dekker likes this.