LooMeenin
LooMeenin

Reputation: 818

How to reference an inactive GameObject in Unity3D?

I'm trying to access an inactive gameobject that is the child of another gameobject, but I'm having trouble.

Right now I'm using this line of code to try to access the inactive child gameobject and set it active:

go.GetComponentInChildren<EnemyMover>().gameObject.SetActive(true);

I am getting a null ref error.

So how can I reference an inactive gameobject?

Upvotes: 2

Views: 3031

Answers (2)

Timo Gr&#228;f
Timo Gr&#228;f

Reputation: 1

I know Im a bit late, but in case somebody is still wondering how to find an inactive GameObject here is an example:

GameObject[] objects = Resources.FindObjectsOfTypeAll();

    foreach (GameObject go in objects)
    {
        if (go.name.Equals("Your component")){
            go.SetActive(true);
        }
    }

Upvotes: 0

31eee384
31eee384

Reputation: 2803

You can use GetComponentsInChildren<EnemyMover>(true), where the true indicates you want to include components on inactive game objects. Then you can use .First() (or any of the variants Linq offers) to get a single result.

Upvotes: 4

Related Questions