Make a Gameobject invisible in Unity

Gameobjects are the core elements of any Unity game. You can add or remove components from gameobjects to change its behavior. You can make it move in any way you like by adding a script or you can make it invisible by changing the renderer properties. In this tutorial, we will see how to make a Gameobject invisible using Unity editor or using code.

How a Gameobject is rendered in Unity

Any Gameobject is visible in the game only if some type of renderer is added to it. Unity uses this renderer class to calculate the 3-dimensional data to display the Gameobject. That might be a little hard to understand. Let’s take an example to understand that.

Say you have a 3D sphere on your scene. Basically, the end display is a 2D image with 3D effect. The renderer takes the 3D data of the Gameobjects and creates a 2D image. The images are shown in such a way that the human eyes perceive it as 3D.

Making a Gameobject Invisible in Unity using mesh renderer

Now that you know how a Gameobject gets displayed in Unity, you might have guessed how to make it invisible. Yes, you just need to disable the renderer. Without a renderer the Gameobject is not taken into account for display. Unity keeps the other component active even if the renderer is disable. You can still move the Gameobject and make it respond to physics.

How to disable the renderer?

  1. Select the Gameobject in the hierarchy window.
  2. Go to the inspector window and look for renderer.
  3. The type of renderer will depend on the type of Gameobject.
  4. Uncheck the renderer to make the Gameobject invisible.
Disable renderer

Disable the renderer using code

You need to get the renderer component using the Getcomponent function and then set the renderer to false.

Here is a code snippet to make the Gameobject invisible on scene start. You need to attach this to the object that you want to make invisible.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Invisible : MonoBehaviour
{
    Renderer test;
    // Start is called before the first frame update
    void Start()
    {
        test= GetComponent<MeshRenderer>();
        test.enabled= false;
    }

}

You can simply set the renderer to true to make the Gameobject visible again. If you are looking to create a transparent Gameobject then you need to create a new material and set the Rendering mode to transparent.

With this you can make your Gameobject visible and invisible at any time. If you have any questions, feel free to leave a comment below.

Making the object invisible using Camera Culling mask

This method is useful if you have multiple cameras and want to make the object invisible only in one of them.

Select the object and go to the inspector window in Unity

Create a new layer called Invisible_layer (You can name it whatever you want)

Now set you object to layer as Invisible_layer

Select the camera in which you don’t want the object to be visible.

Go to inspector window>Culling mask and untick the Invisible layer.

Now the object should no longer be visible in the scene.

If you want to do this using code during runtime, use the code below.

using UnityEngine;

public class Cullingmaskexample : MonoBehaviour
{
    LayerMask invisible_layer_mask;
    void Start()
    {
        invisible_layer_mask=LayerMask.NameToLayer("Invisible_layer");
        invisible_layer_mask=~invisible_layer_mask;//This inverts the value
        Camera.main.cullingMask= 1<<invisible_layer_mask;
        
    }

  
}

6 thoughts on “Make a Gameobject invisible in Unity”

  1. If I have two cameras (one for player 1 and one for player 2), is it possible to code the gameObject’s renderer to be turned off for one camera while still on for the other?

    Reply
    • This can be done using layers and a culling mask. Move the object you want to hide in one of the cameras to a different layer. Then use a culling mask to render the layers you want. I will add this to the tutorial shortly.

      Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from VionixStudio

Subscribe now to keep reading and get access to the full archive.

Continue reading