Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Finding a deactivated GameObject in order to activate it

Discussion in 'Scripting' started by Larpushka, Aug 1, 2015.

  1. Larpushka

    Larpushka

    Joined:
    Jan 6, 2015
    Posts:
    214
    Hi guys,
    So I'm using the ol' GameObject.FindGameObjectWithTag command, followed by SetActive(true)

    Problem is, the Find command never finds my gameobject because it's in a disabled state! (i.e. the checkbox in the inspector is unmarked).
    For the record, the "gameobject" I'm referring to is actually a UI text with a button that I want to activate at a certain point. Basically what I'm asking is how can I find gameobjects that are unchecked (deactivated) in the inspector?

    And if I can't, then how would you recommend I solve my issue?
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Couple of solutions:

    - Leave the objects enabled then do your FindGameObjectWithTag() in Awake(), store the reference and SetActive(false) to disable them via code.

    - Loop through the entire hierarchy and test every GameObject. This is the worse idea.
     
  3. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Typically speaking if you have some object that's just sitting there doing absolutely nothing, it'll have some sort of immediate superior (manager) that controls its activation and the like. In those cases, you can just drag the reference onto the manager script in the inspector and then call a function on the manager to activate it at some point in runtime.

    Other times, it might be deactivated because it's simply too far away to be useful. Keep in mind that one of the few things that can easily reactivate a disabled GameObject is a Trigger- triggers will still fire, giving the GameObject the opportunity to wake itself up if appropriate.
     
  4. Larpushka

    Larpushka

    Joined:
    Jan 6, 2015
    Posts:
    214
    Thanks a lot GoZZleR, your first solution worked like a charm. I appreciate the feedback of the other solution, Lysander, I might use it when my Unity skills get more advanced.