- Home /
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?
also a side note, what is the correct usage of something like this:
SeatLoc = transform.FindChild("ChairSeat").transform; OR SeatLoc = transform.FindChild("ChairSeat");
It doesnt appear to have an effect if I keep the transform at the end or if I remove it.
You should recursively call your findBone() function. Inside the function once you have a child call findBone(child) so you search down the tree.
oh, and the transfrom.find() is relative to the current bone, so take a look at what the name of your Character.transform is, and make sure that does not appear in the path that you are searching for.
Answer by sneftel · Aug 04, 2011 at 01:32 PM
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.
I thought this might be the case as well, but right before the update function i tried putting these debugs:
Debug.Log("SeatLoc.position = " + SeatLoc.position); Debug.Log("HandLoc.position = " + HandLoc.position); Debug.Log("RootBone.position = " + RootBone.position); Debug.Log("HandBone.position = " + HandBone.position);
and it prints out the position of all of them except the HandBone.position which gives another NullReferenceException
Probably because you capitalized HandBone
differently than when you set it.
lol, I thought I had fixed all of those capitalization mistakes, except i missed the most important one. Now my recursive function and the bone 'path' are both working
I will post my new recursive function as an answer for those interested
Answer by Heiko · Aug 04, 2011 at 03:53 PM
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;
}
Little addition: if (current.name == name) --> if (current.name.Contains(name)) A Bip often has extra info in the name, so this method helps you to work more generic.
Answer by jheffel · Aug 04, 2011 at 04:15 PM
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;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612165759im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do i find a bone on my character within a script? 2 Answers
find next gameobject in hierarchy? 1 Answer
Adding extra bones to an already imported and prefab'ed character. 1 Answer
How can I find a unique sibling/parent/child in a specific hierarchy and not from all the scene? 1 Answer
Adding new bones to a character 1 Answer