Accessing Child GameObject

I know there must be a simple solution to this, but I can’t seem to figure it out. I have an object attached to an NPC, and I want to access it so as to enable and disable it through the script on the parent. How do I go about this?

If it is an immediate child, or if you know the path from the parent to the child, you can use [Transform.Find()][1]. [1]: http://docs.unity3d.com/Documentation/ScriptReference/Transform.Find.html

This is a possible duplicate of this question: http://answers.unity3d.com/questions/10417/how-can-i-access-the-children-of-a-transform.html There's a pretty good answer there, see if it works for you :)

2 Answers

2

Try this

Transform aChild = transform.FindChild( "child name" );

if( aChild != null )
{
        Debug.Log( "Found child" );
        aChild.gameobject.SetActive( true ); // false
 }
else Debug.Log( "Child not found" );

Attach it to the parent