How to get a recursive named bone transform?

Hi I’m trying to get the transform of a hand bone that is deep in the bone hierarchy. I’ve tried many different ways to access it but haven’t found one that works yet.

It tried writing this function to go through all of the bones but it seems to only grab the immediate children of the root bone.

function findBone(parent: Transform) : Transform{
	for(var child : Transform in parent){
		print("Found bone: " + child.name);
		if (child.name == "CharacterSkeleton R Hand"){
			return child;
		}
	}
}

I have also tried putting in the path to get the specific hand bone. Is this not working because there are spaces in the bone names?

Handbone = Character.transform.Find("CharacterSkeleton/CharacterSkeleton Pelvis/CharacterSkeleton Spine/CharacterSkeleton Spine1/CharacterSkeleton Spine2/CharacterSkeleton Spine3/CharacterSkeleton Neck/CharacterSkeleton R Clavicle/CharacterSkeleton R UpperArm/CharacterSkeleton R Forearm/CharacterSkeleton R Hand").transform;

in both cases I get a nullReferenceException on this line:

HandLoc.position = HandBone.position;

Where I am guessing HandBone.position is null.

Here is the script in its entirety:

private var HandBone : Transform;
private var HandLoc : Transform;
private var RootBone : Transform;
private var SeatLoc : Transform;
var Character : GameObject;

Debug.Log("Using Character: " + Character.name);
RootBone = Character.transform.FindChild("CharacterSkeleton").transform;
Handbone = Character.transform.Find("CharacterSkeleton/CharacterSkeleton Pelvis/CharacterSkeleton Spine/CharacterSkeleton Spine1/CharacterSkeleton Spine2/CharacterSkeleton Spine3/CharacterSkeleton Neck/CharacterSkeleton R Clavicle/CharacterSkeleton R UpperArm/CharacterSkeleton R Forearm/CharacterSkeleton R Hand").transform;
//Handbone = findBone(RootBone);

SeatLoc = transform.FindChild("ChairSeat").transform;
HandLoc = transform.FindChild("ChairHand").transform;

function Update (){

	SeatLoc.position = RootBone.position;
	HandLoc.position = HandBone.position;
} 

function findBone(parent: Transform) : Transform{
	for(var child : Transform in parent){
		print("Found bone: " + child.name);
		if (child.name == "CharacterSkeleton R Hand"){
			return child;
		}
	}
}

If I comment out all of the hand code, the RootBone code works perfectly.
Any ideas on how to access the hand bone?

You are missing the recursive step, in C# it would be like this:

public Transform SearchHierarchyForBone(Transform current, string name)   
{
    // check if the current bone is the bone we're looking for, if so return it
    if (current.name == name)
        return current;

    // search through child bones for the bone we're looking for
    for (int i = 0; i < current.GetChildCount(); ++i)
    {
        // the recursive step; repeat the search one step deeper in the hierarchy
        Transform found = SearchHierarchyForbone(current.GetChild(i), name);

        // a transform was returned by the search above that is not null,
        // it must be the bone we're looking for
        if (found != null)
            return found;
    }

    // bone with name was not found
    return null;
}

here is the fixed recursive function for those interested, I had capitalization mistakes on the HandBone variable, and now both methods work.

function findBone(parent: Transform) : Transform{
	for(var child : Transform in parent){
	
		print(parent.name + " found bone: " + child.name);
		
		if (child.name == "CharacterSkeleton R Hand"){
			Debug.Log("Found Hand, final return");
			return child;
		}
		else{
			var tempchild : Transform = findBone(child);
			Debug.Log("current bone: " + child.name + ", tempchild: " + tempchild.name);
			//if(tempchild.name != child.name){
			
				if(tempchild.name == "CharacterSkeleton R Hand"){
					Debug.Log("Found Hand, return to parent");
					return tempchild;
				}
			//}
			else{
			Debug.Log("Didnt find hand, returning to parent: " + parent);
				//return parent;
			}
			
		}
		
	}
	return parent;
}

If Handbone = Character.transform.Find("...").transform itself isn’t giving you a NullReferenceException due to accessing the transform property, then you’re finding the hand bone correctly. My guess is that it’s the chair you’re not finding.