- Home /
 
Set child gameobject active?
How can you set the child of a gameobject active I have tried
 gameObject.transform.child.gameObject.SetActive(true);
 
               But I get the error
NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type[] cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cacheKeyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetProperty (System.Object target, System.String name) UnityScript.Lang.UnityRuntimeServices.GetProperty (System.Object target, System.String name) BuildZone.Update () (at Assets/my assets/Scripts/BuildZone.js:24)
the script was attached to the parent of the object I was trying to make active How can I make the object active?
Answer by ArkaneX · Nov 16, 2013 at 10:57 AM
Transform has no child property, so this should not even compile...
To access children of a Transform, please use:
 for (var child : Transform in transform)
 {
     if(....) // check if this is the child you want to activate, by name, tag or anything else
     {
         child.gameObject.SetActive(true);
     }
 }
 
               If you have one child only, then of course you can skip the if statement. You can also access a child by its index using GetChild method.
Assets/my assets/Scripts/BuildZone.js(24,33): BCE0005: $$anonymous$$ identifier: 'child'. is this C#?
Nope - it's javascript. Could you post related lines from your code?
here is part of the script
 function Update () 
     {
         if(inBuildZone)
         {
             if(INV.sticks >= 30 && Input.Get$$anonymous$$eyDown ("r"))
                 {
                      Debug.Log("Build house");
                     child.gameObject.SetActive(true);
                 }
         }        
     }
 
 
                  the Debug.Log is called. does it matter if the game object with the "child.gameObject.SetActive(true);" is also a child?
Did you declare/assign child variable? In the sample code from my answer, child is both declared and assigned as part of for loop.
And regarding your question - it doesn't matter.
Your answer