Reputation: 818
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
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
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