Attach object to avatar bone

Hi,

I’ve looked through the unity answers and although some of the questions in reference to this help, none of them seem to hit exactly what I need. I have a gameobject that I currently want to attach to the head bone of the character to be a hat. I would just attach it in the hierarchy and render visible in some cases, but the problem is that my character isn’t in the hierarchy until the game is started. There is a selection screen where the user gets to choose which avatar they want to use. Because of this I’m not sure how to tell an object to find another object, then find the specific bone it needs to attach to.

Thanks!

Answer from “farfarer” http://forum.unity3d.com/threads/92945-Attach-gameobject-to-avatar-bone

If the head bone name never changes,
you can just call it, get it’s
transform and set the parent of your
hat to be that transform…

public var hat : Transform;

function Start () {
hat.parent = gameObject.Find("HeadBone").transform; // Attach hat to head bone.
hat.localPosition = Vector3(0, 0, 0); // Set local position so that hat sits on top of the head.
hat.localRotation = Quaternion.Identity; // Zero hat's rotation.
}