Getting a childs child.

Hi All,

I need to access the children of an object. This was all fine when i was using

script = GetComponentInChildren();

to access the animation in a child.

However this gameobject has now been dropped down one level and the script is no longer being able to find the animation.

old hierarchy:
gameObject
– Game Object with animation to get

new hierarchy:
gameObject
– new gameObject (this is an empty gameobject if that helps with the answer)
– Game Object with animation to get

My question is how do i access the animation from the childs child from the top level game object?

Cheers

Nade

You could try and use one of these.

GameObject.Find
GameObject.FindWithTag

Children are made children via the Transform. Transform’s have a ‘Find’ and ‘FindChild’ method (they both act similar, I believe one is deprecated). This is similar to GameObject.Find, but instead of working in the global world, you’re looking in the specific GameObject whose transform you call it on, excluding all other GameObjects in the world.

Transform.Find

This allows you to find a child by putting in path name to the child. Thinking of each child as a folder. So if hand is a child of arm is a child of the transform you’re searching, you’d say:

transform.Find("arm/hand");

Note, if you don’t know the directory path to the child you can use a recursing loop to find the child by name or tag. But this is probably going to be far slower, and has a chance of returning the wrong child if two children have the same name.

You could even go as far to write your own query string interpreter for this that would allow you to say something like:

spine/rightarm/*/hand
leaving out the path down the rightarm

or

*/hand
finding any hand that is a child of this

something like this:

        public static Transform SearchChild(this Transform trans, string spath)
        {
            if (!spath.Contains("*"))
            {
                return trans.Find(spath);
            }
            else
            {
                var arr = spath.Split('/');
                string sval;
                spath = "";

                for (int i = 0; i < arr.Length; i++)
                {
                    sval = arr[i];

                    if (sval == "*")
                    {
                        if (spath != "")
                        {
                            trans = trans.Find(spath);
                            if (trans == null) return null;
                            spath = "";
                        }

                        i++;
                        if (i >= arr.Length)
                            return (trans.childCount > 0) ? trans.GetChild(0) : null;
                        else
                        {
                            sval = arr[i];
                            //now we're going to do our recursing search
                            trans = Search(trans, sval);
                            if (trans == null) return null;
                        }
                    }
                    else
                    {
                        if (spath != "") spath += "/";
                        spath += sval;
                    }

                }

                return trans;
            }
        }

        public static Transform Search(this Transform trans, string sname)
        {
            foreach (Transform child in trans)
            {
                if (child.name == sname) return child;
            }

            foreach (Transform child in trans)
            {
                var nxt = Search(child, sname);
                if (nxt != null) return nxt;
            }

            return null;
        }