Search Unity

How to make an object visible/invisible in Unity?

Discussion in '2D' started by NoVate911, Jan 20, 2020.

  1. NoVate911

    NoVate911

    Joined:
    Jan 1, 2020
    Posts:
    13
    Hello!
    Tell me how to make a visible and invisible object?
    I know that everyone should know this, but something has flown out of my head!
    • For example, I want that when you click on the button, the text becomes invisible, and if you click again, the text will become invisible!
    Thanks to everyone in advance!
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @OrganFreman

    Sometimes you have to set active the whole game object but in case of UI text, you can also enable/disable just that component script. That ways the gameobject is still "active" only the text isn't drawn.

    Code (CSharp):
    1. if (something)
    2. {
    3.     yourTextComponent.enabled = false;
    4. }
    5. else
    6. {
    7.     yourTextComponent.enabled = true;
    8. }

    See:
    https://docs.unity3d.com/2018.3/Documentation/ScriptReference/UI.Text.html
    https://docs.unity3d.com/2018.3/Documentation/ScriptReference/Behaviour-enabled.html
     
  3. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    691
    Any Renderer component can be enabled or disabled to show/hide. This is often preferable to setting the whole GameObject active/inactive, but that depends on your case. Note that GameObject.Find() will not work on inactive GameObjects...though you should rarely, if ever, use that function anyways...better to have a public slot for the GameObject and drag it in the Inspector.

    Also note that if you have a Collider, but set the Renderer to disabled, it will still register collisions, but be invisible! So you'd need to disable the Collider as well, usually.
     
    Tonymotion likes this.