How to get component of an inactive gameobject?

I am trying to get the component of an inactive gameobject. The gameobject that I want to get the component of is a child object. All other child objects also have the same component. But I only want to get the component of the inactive gameobject. I have tried tagging the inactive gameobject and finding the tag and getting its component. But GetComponent does not get the component of an inactive gameobject.

This is the line of code that I tried:

buildingPlacement = GameObject.FindGameObjectWithTag(“InactiveSign”).GetComponent();

GetComponent does get components from inactive gameobjects. However FindWithTag (like most Find methods) does not find inactive gameobjects. Note that there is no FindGameObjectWirhTag method, just a FindGameObjectsWithTag which however returns an array of gameobjects and not a single one. Though even that will not find inactive gameobjects as you can read in the documentation.

Inactive gameobjects do not participate in any activities in your scene. The only way to actually “find” inactive gameobject is through Resources.FindObjectsOfTypeAll.

You should generally avoid using any of those Find methods. It’s generally better to setup references beforehand in the inspector.

If the gameObject exists at edit time and you know which one you want :

 // Drag & drop in the inspector the gameObject
public TheComponentYouWant TheComponent;

private Foo()
{
     theComponent.DoSomething();
}

If the component is attached to a direct child gameObject as you have indicated, but you don’t know at edit time which one will be the deactivated one:

 private Foo()
 {
     TheComponentYouWant theComponent = null;

     int childCount = transform.childCount;
     for( int i = 0 ; i < childCount && theComponent == null ; ++i )
     {
         GameObject child = transform.GetChild( i ).gameObject ;
         if( !child.activeSelf )
             theComponent = child.GetComponent<TheComponentYouWant>();
     }

     if( theComponent != null )
         the component.DoSomething();
 }

GetComponent<>() should work on inactive gameObject, but FindGameObjectWithTag will find only active gameobjects. There are ways to find inactive gameObjects e.g. reference it in the scene or others: