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.
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.
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.
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?
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”.
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.
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.
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.