GetComponentInChildren of inactive objects

Followed the example here in the documentation verbatim:

Unitys example code does not compile:

HingeJoint hingeInactive = gameObject.GetComponentInChildren( typeof(HingeJoint), true ) as HingeJoint;

The error is:
error CS1501: No overload for method GetComponentInChildren' takes 2’ arguments

How to retrieve a component that exists in a hierarchy of gameobjects which may or may not be active?
Thank you

Edit:
Apparently this is a new API for 5.3 that can do this. I have 5.2.3 so this isn’t available for me.

1 Like

I’m sure this or a simmilar solution exists hundreds of times on the forums.

public static Component SearchForComponent<T>(GameObject go)
   {
       //Debug.Log ("Searching " + go.name);
       Component c1 = go.GetComponent<T>() as Component;
       if (c1 != null)
       {
         return (c1);
       }

       Transform t = go.transform;
       for (int i = 0; i < t.childCount; i++)
       {
         Transform child = t.GetChild (i);
         Component c2 = SearchForComponent<T>(child.gameObject) as Component;
         if (c2)
         {
           return (c2);
         }
       }
  

     return (null);

   }

Finds the first occurance of the component of type T in a tree. This only used during Awake or Start so performance is not important.
I’ll use this until I can have a chance to install 5.3