Search Unity

GetComponent of disabled object

Discussion in 'UGUI & TextMesh Pro' started by any_user, Oct 21, 2014.

  1. any_user

    any_user

    Joined:
    Oct 19, 2008
    Posts:
    374
    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.
     
    Last edited: Oct 21, 2014
    BMRG14, john_unity389 and AzureByte like this.
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    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
     
  3. any_user

    any_user

    Joined:
    Oct 19, 2008
    Posts:
    374
    Sorry I didn't mean GetComponent<T>(), but all the other ones, just confused it when writing.

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

    MrLucid72

    Joined:
    Jan 12, 2016
    Posts:
    996
    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?
     
    Deleted User, akuno and cxode like this.
  5. damdor

    damdor

    Joined:
    Nov 7, 2016
    Posts:
    4
    GetComponentInChildren has overloaded version with parameter "includeInactive"
     
  6. MrLucid72

    MrLucid72

    Joined:
    Jan 12, 2016
    Posts:
    996
    You rock man this is exactly it
     
    Capzeze, akuno and Pablomon like this.
  7. Zullar

    Zullar

    Joined:
    May 21, 2013
    Posts:
    651
    I have a bug where GetComponentInParent<T> is failing to find a component.

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

    Shouldn't GetComponentInParent<T> and transform.parent.GetComponent<T> 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".
     
    CloudyVR likes this.
  8. Adjuvant

    Adjuvant

    Joined:
    Mar 2, 2015
    Posts:
    2
    Bump this, would be good to have same overloads on all types of GetComponent(s) in self, parents and children.
     
  9. Zullar

    Zullar

    Joined:
    May 21, 2013
    Posts:
    651
  10. samizzo

    samizzo

    Joined:
    Sep 7, 2011
    Posts:
    487
    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.
     
  11. soulburner

    soulburner

    Joined:
    Mar 25, 2012
    Posts:
    169
    You should use GetComponentsInParent(true), it can take [true] as a parameter.
     
  12. Estecka

    Estecka

    Joined:
    Oct 11, 2013
    Posts:
    62
    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.
     
    koriball, a436t4ataf and JonnyHilly like this.
  13. Pablomon

    Pablomon

    Joined:
    Mar 9, 2015
    Posts:
    47
    Awesome!
     
  14. juansosantacoloma

    juansosantacoloma

    Joined:
    Nov 22, 2020
    Posts:
    1
  15. canyon_gyh

    canyon_gyh

    Joined:
    Aug 15, 2018
    Posts:
    48
    I have a bug where GetComponentInParent<T> is failing to find a component.when the gameObject is not active and dynamic bind <T> to the gameObject。Then GetComponentInParent is null.
    Thianks !
     
  16. kaiyum

    kaiyum

    Joined:
    Nov 25, 2012
    Posts:
    686
    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 https://gist.github.com/kaiyumcg/0d053969ada9297bcb58375879ced0c0
    using GetComponentsInParent since it works.
     
    mdrunk likes this.