Hiding and Un-hiding Objects

From ETC Public Wiki
Jump to: navigation, search

There are several ways to hide an object in Unity, each with their own differences. In turn, these various techniques can be grouped into three means: modifying the properties of the renderer of an object , changing its material, or deactivating the GameObject itself. See below for details on how to do all three:


The Renderer class has the boolean property enabled; setting it to false will cause the object to no longer render (and thus be invisible). That said, the object will still exist and interact with the rest of the game: it will trigger collisions etc., but it will no longer cast (or receive) shadows (even if the appropriate settings are enabled). Note that a GameObject does not necessarily always have a Renderer assigned to its renderer field, but if your GameObject contains any kind of renderable geometry, it most likely was automatically given a Renderer by the engine.

renderer.enabled = false;

Naturally, by setting the property to true again, the object will become visible. Note that the Renderer object has several other properties that may be of value when trying to determine how exactly a particular object should visibly behave. For more information, see its Script Reference: http://unity3d.com/support/documentation/ScriptReference/Renderer.html


You can access the Material of an object's Renderer simply through the Renderer's material property. Accessing a Material in this fashion (as in, via it's Renderer) will result in that Material being changed only for this object. If the material is used by any other Renderers, the shared Material will be cloned, and this reference will start using said clone from now on.

To make the Material disappear (and thus the object as well), set the color of the Material to have an alpha value of 0. You can do this by simply providing a new Vector4 in the format of r,g,b,a (red, green, blue, alpha). Note that all of these values are on a 0-1 scale, where 0 would be completely transparent (for alpha) and 1 would be completely opaque (for alpha).

renderer.material.color = new Vector4(1,1,1,0);

However, this method will not work unless the Material's shader supports transparency. Fortunately, Unity has a several built-in transparent Shaders. If an object does not currently have a transparency enabled Shader, you may want to swap the Material on the fly (see the Swapping Materials and Textures section on how to do this). For more information on Materials, see its Script Reference: http://unity3d.com/support/documentation/ScriptReference/Material.html

Deactivating the Object

You can completely deactivate a GameObject by settings its active property to false. The GameObject should no longer be visible or interact with the rest of the game environment. This is almost like destroying an object, but it will still exist and can "come back to life" later. Note that once an object is deactivated, it will no longer continue to execute code either (thus, it cannot directly reactivate itself!).

active = false;