How to make an object visible/invisible in Unity?

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!

@NoVate911

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.

if (something)
{
    yourTextComponent.enabled = false;
}
else
{
    yourTextComponent.enabled = true;
}

See:

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.

1 Like