Trouble referencing a bone transform in Unity. (Using MakeHuman armature / configured in Avatar ok / Anim mocaps working ok))

by Super MegaBroBro   Last Updated November 26, 2017 02:13 AM

I've seen that the bones are Transforms. And i've tried copying many examples. The armature is the standard MakeHuman one and is unchanged. It imported to Unity fine and the mocap animations work perfectly. What I need to do is make the hand a parent of my gun .

Here is what I have currently (I have tried many similar permutations):

    Transform handR;
public GameObject gunPrefab;
GameObject gun;

private void Start()
    rb = GetComponent<Rigidbody>();
    playerAnims = GetComponentInChildren<PlayerAnimations>();
    absoluteGround = rb.transform.position.y;
    distToGround = GetComponent<Collider>().bounds.extents.y;
    handR = transform.Find("hand_r");
    Debug.Log("handR really is: " + handR);

    gun = GameObject.Instantiate(gunPrefab);
    //gun.transform.position = handR.position;
    gun.transform.rotation = Quaternion.identity; 

picture showing imported bones structure

The end result of this: The gun will spawn but isnt parented to anything. And if I try to set its position using handR.position then i get null exception. Pic above shows the image where I see its called "hand_r"

One more thing of note: The bones shown in the image above , do NOT appear in the hierarchy. I have on occasion imported models with the bones included whose bone names all appear in the hierarchy - could this be the reason Unity is finding "hand_r"??? and if so whats the best way to fix that (Please done say remove the armature and make my own in Blender !! :O )

PS: I have since tried it with a new model, made in Blender with a Rigify rig. The code part "handR = transform.Find("hand_R"); -yes the rigify rig names it with capital "R". Still doesnt work, BUT THERE IS A WORKAROUND I FOUND... if I make the Transform handR public and drag it into the Inspector it all works fine! (And yes the Rigify skeleton also shows up in hierarchy!) (So if you are reading this and struggling too, DONT use the MakeHuman skeleton, use one from Blender Rigify.)

But I'm still very confused why the "Find" command doesnt find the bone

Answers 1

Have you tried reading about transform.find on the scripting Api?

"public Transform.Find(string name)

Finds a child by name and returns it.

IF NO CHILD WITH NAME can be found, null is returned. If name contains a '/' character it will access the Transform in the hierarchy like a path name.

Note: Find does not perform a recursive descend down a Transform hierarchy."

Here is an example of its usage:

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
public GameObject player;
public GameObject gun;
public Transform ammo;

//Invoked when a button is clicked.
public void Example()
    //Finds and assigns the child of the player named "Gun".
    gun = player.transform.Find("Gun").gameObject;

    //If the child was found.
    if (gun != null)
        //Find the child named "ammo" of the gameobject "magazine" (magazine is a child of "gun").
        ammo = gun.transform.Find("magazine/ammo");
    else Debug.Log("No child with the name 'Gun' attached to the player");

It's not working in your case because of its wrong usage inside the script.

November 26, 2017 09:00 AM

Related Questions

How to set default scale on a per bone basis

Updated July 14, 2015 13:05 PM

Workflow for non human ragdolls

Updated August 03, 2016 08:05 AM