Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Animator.GetBoneTransform() returns null

Discussion in 'Animation' started by Asse1, May 10, 2017.

  1. Asse1

    Asse1

    Joined:
    Jan 9, 2013
    Posts:
    89
    Hey folks,

    we have a rigged character with a humanoid skeleton. Everything is setup correctly and when I switch to the configuration view I can also see that every human body bone has its corresponding Transform.

    Now I need to attach another GameObject to one of these bones and since Unity doesn't allow me to find these bones by using Find() or whatever my idea was to use Animator.GetBoneTransform() to retrieve the bone I need.

    But the problem is that this method always returns null, no matter what HumanBodyBones I use.

    We tried disabling OptimizeGameObjects and OptimizeMesh for the model importerex but that didn't help.

    Also the ExtraTransformsToExpose list doesn't expose any Transform so I could use these.

    Any suggestions?
     
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,462
    You can use GameObject.Find() but this is literally the worst possible solution. If Find() couldn't even find the bone by name then you're probably breaking something on your rig/animator/avatar at runtime.

    Animator.GetBoneTransform() is the safest way to get a particular bone on a humanoid rig and if the avatar is configured correctly and assigned to the mesh then there is probably something breaking things at runtime or you're running GetBoneTransform() on the wrong animator.

    Maybe post some code or share the model.
     
  3. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    I've never tried using GetBoneTransform before, but it seems to always be returning null for me as well. I'm positive I have the correct animator referenced and that my Mecanim rigs are configured correctly.

    Anyone else running into this? I'm using 2017.1. I'll try testing with an empty project tonight.
     
  4. james_unity988

    james_unity988

    Joined:
    Aug 10, 2018
    Posts:
    71
    I am also having this problem. The rig is properly imported as Humanoid and I have confirmed that all the transforms that I am requesting are part of the rig. I have double-checked that the animator I am calling is indeed the correct animator. I am using 2018.2.1f1 currently and I'm tempted to try the beta 2018.3
     
  5. warmanw

    warmanw

    Joined:
    Feb 2, 2015
    Posts:
    32
    Hi guys, we experience same issue. we have 2 rigs one works perfectly and another with same hierarchy and setup just return null when I call
    GetBoneTransform(HumanBodyBones.AnyPart)


    We are able to find the bones with GamObject.Find() but not with GetBoneTransform()

    2018.3.6
     

    Attached Files:

  6. teasully

    teasully

    Joined:
    Nov 26, 2016
    Posts:
    17
    I figured out the issue. If you rename any of the GameObjects in the rigs hierarchy, GetBoneTransform will return null. Make sure to have the names the same as they are in the Avatar component.

    Maybe you can change the name(s) but you will also have to update the name changes in the Avatar object aswell.
     
    darkriver41, TorbenDK and ericiba like this.
  7. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,193
    I was running into this issue today, where GetBoneTransform was always returning null on one of my models. This probably won't be the reason most will encounter, but I figured I'd mention my issue, and what fixed it for me.

    In my case, the issue was that I swapped out a model, but didn't update the Animator's avatar. So Unity was trying to use the wrong avatar to find the bones on my new model. This was fixed by making sure that the Animator component on my character was pointing at the correct avatar.
     

    Attached Files:

    cybie likes this.
  8. WaqasGameDev

    WaqasGameDev

    Joined:
    Apr 17, 2020
    Posts:
    118
    Also please ensure the gameobject with animator is enabled when you call GetBoneTransform.
     
    kloogens and fjolne like this.
  9. QuarterMillion

    QuarterMillion

    Joined:
    Jul 18, 2020
    Posts:
    10
    You can also use transform.Find() in tandem with a known path to the transform you want if you know the exact path to the transform in question.

    Its in the unity documentation.
     
  10. RikiSpanish

    RikiSpanish

    Joined:
    Jan 26, 2023
    Posts:
    1
    I had the same issue as well. I found my error to be that the avatar I created was Animation type Generic instead of Humanoid. Once I created the new avatar type Humanoid and updated all my animations to use this new avatar, I was able to call GetBoneTransform.

    Make sure the avatar type you are using is Humanoid and if not, update it. Hopefully this helps :)