GetComponent of disabled object

I know that GetComponent (including GetComponentInChildren, GetComponentInParent in 4.6) only return active objects. Is there a way to get one inactive component without using GetComponents(true)[0] to avoid allocating memory of the returned component array?

Edit: I mean only GetComponentInChildren and GetComponentInParent - GetComponent finds components even if the gameobject is inactive.

3 Likes

There’s no such thing as inactive components; only GameObjects can be active or not. Components can be disabled, but GetComponent does return disabled components. It also returns components of inactive GameObjects.

–Eric

7 Likes

Sorry I didn’t mean GetComponent(), but all the other ones, just confused it when writing.

GetComponentInChildren() and GetComponentInParent() only return components in active gameobjects. I specifically had the problem with the new GetComponentInParent in 4.6.

4 Likes

Necro bump - 100% relevant to me and 1st link from Google.

I discovered this bug as well. Normally I can GetComponent for objects that are not set active. However, for getting the children, it doesn’t return any results. IS this a bug, or am I doing something wrong?

3 Likes

GetComponentInChildren has overloaded version with parameter “includeInactive”

69 Likes

You rock man this is exactly it

3 Likes

I have a bug where GetComponentInParent is failing to find a component.

GetComponentInParent returns null //BUG!!!
transform.parent.GetComponent returns the component properly

Shouldn’t GetComponentInParent and transform.parent.GetComponent behave identically? I’m unsure why the former is buggy, but the latter works properly. Bug is possibly related to UNET and how objects are disabled briefly during initialization until “spawned”.

1 Like

Bump this, would be good to have same overloads on all types of GetComponent(s) in self, parents and children.

5.6.1f1
Unity QA replicated the bug where GetComponentInParent returns Null but transform.parent.GetComponent works correctly.
https://fogbugz.unity3d.com/default.asp?932531_okip5dh462jnf23q

So it seems the real bug here is that GetComponentInParent is only searching active GameObjects. It would be great if GetComponentInParent had an overload like GetComponentsInChildren where you can choose to include inactive GameObjects.

4 Likes

You should use GetComponentsInParent(true), it can take [true] as a parameter.

4 Likes

Thanks !
Written plainly, this.GetComponentInParent<T>() becomes this.GetComponentsInParent<T> (true)[0]
That’s not too ugly, but I still wish the single version had an overload to take disabled objects

Not exactly. transform.parent.GetComponent will only search for the first parent. GetComponentInParent will also search every grand-parents.

3 Likes

Awesome!

GetComponentInChildren(typeof(ParticleSystem), true) this works

I have a bug where GetComponentInParent is failing to find a component.when the gameObject is not active and dynamic bind to the gameObject。Then GetComponentInParent is null.
Thianks !

Stumbled across this issue just now. GetComponentInParent is buggy, it does not have any overloaded version that can let us control whether we want inactive gameobject’s component or not. The default implementation will not find any component attached to gameobjects that are inactive. So the solution Find component in disabled parent gameobjects · GitHub
using GetComponentsInParent since it works.

1 Like