Hi,
I’m trying to get a reference to an animated character’s root bone, without using hardcoded object names (i.e. Transform.Find()). Looking at the SkinnedMeshRenderer in the inspector, I can see there is a field called “Root Bone” which is set to point at exactly what I want. But I don’t know how to access this from a script, or even if it is possible. It isn’t documented in the script reference for SkinnedMeshRenderer, and in the screenshot in this article there is no Root Bone in the inspector. Presumably this is something that’s been added in a recent version? I tried just reading the guessed variable/property name rootBone, RootBone etc., no luck.
It’s possible, but I don’t know if there’s a direct way, here’s some code from my skinnings experiment that should get you close. Obviously you’d just do this once and save the reference.
bones = new Transform[prefabRenderer.bones.Length];
Transform[] transforms = transform.root.GetComponentsInChildren<Transform>();
int boneCount = bones.Length;
for (int bone = 0; bone < boneCount; bone++) {
string boneName = prefabRenderer.bones[bone].name;
bones[bone] = System.Array.Find(transforms, trans => trans.name == boneName);
}
// Save the hierarchy of the skeleton
Transform skelRoot = bones[0];
parents = new int[boneCount];
for (int bone = 0; bone < boneCount; bone++) {
parents[bone] = System.Array.IndexOf(bones, bones[bone].parent);
if (parents[bone] < 0)
skelRoot = bones[bone];
}