How to Enable and Disable GameObjects in Unity Using C# | SetActive() Method Tutorial

Introduction – Important information about GameObjects

GameObjects form the fundamental building blocks of every Unity scene, with each one containing at least a Transform component by default. This essential component tracks the object’s position, rotation, and scale within the 3D world. To create functional elements in your game, you extend these basic GameObjects by adding specialized components that define their behavior and properties. For instance, a camera in Unity is essentially a GameObject enhanced with both Camera and AudioListener components, transforming a simple object into the player’s viewport and audio receiver.

empty gameobject in unity to test activation and deactivation
Fig. 1: A test GameObject in the hierarchy.

When a GameObject is active in the hierarchy and the game is running, Unity periodically updates each of its components and executes some other functions, all this automatically. If we deactivate the GameObject, this process will not take place and we will achieve the effect that the GameObject disappears from the scene.

During runtime, Unity automatically processes all active GameObjects in your hierarchy, executing their component update functions each frame according to their scripting implementation. When you disable a GameObject by setting it to inactive, Unity suspends this update process for both the object and all its children, effectively removing it from the scene rendering pipeline. This not only makes the GameObject visually disappear but also prevents its components from consuming processing resources—an important optimization technique for managing scene performance and object lifecycles.

For a visual breakdown of this subject, you may find my video discussion helpful:




How to manually activate and deactivate objects in Unity

As previously noted, every GameObject in Unity must contain at least one Transform component—this is mandatory and cannot be removed. When you select a GameObject in the Hierarchy window, the Inspector panel will display its Transform properties along with the header section (visible in Figures 2 and 3).

gameobject inspector with the mouse pointer in the activation box, checkbox
Fig. 2: Activation checkbox of the GameObject, at this time it is active.
gameobject inspector with the mouse pointer in the activation box, unchecked box
Fig. 3: Activation checkbox of the GameObject, at this moment it is inactive.

The checkbox highlighted in both images is the GameObject’s active state toggle. When enabled (checked), the GameObject remains active in the scene. When disabled (unchecked), the GameObject becomes inactive—effectively behaving as if it were removed from the scene. In the Hierarchy window, inactive GameObjects appear visually muted, as shown in Figure 4.

unity hierarchy with inactive gameobject
Fig. 4: Inactive GameObjects are shown in gray in the hierarchy.



Activate and Deactivate GameObject using Script

Of course we could get more out of this if we could activate and deactivate one or more GameObjects through a Script, this would allow us for example to make enemies appear when the player reaches a certain part of the map, make a final screen appear when the player loses the game and more.

Let’s start by creating a new Script and GameObject in the hierarchy to which to assign the Script.

unity csharp script
Fig. 5: We created a new script to make the activation and deactivation of the GameObject by code.
unitys hierarchy
Fig. 6: We create an extra GameObject to assign the script we just created.

In figure 7 you can see that the new GameObject is assigned the script we created. This can be done by dragging the script directly or by using the Add Component button and searching for the script by name.

inspector of a gameobject with a script created by the assigned user
Fig. 7: We assign the script to the GameObject created in the previous step.

To be able to activate or deactivate the GameObject from a Script we need to have the reference of the GameObject we want to activate or deactivate, so inside our Script we are going to define a public GameObject, in this case I have called it “gameObjectToDeactivate”, trying that the name reflects as much as possible the function it is going to perform.

reference for a public gameobject in c sharp unity
Fig. 8: We define a public reference for a GameObject and give it a representative name.

When defining the public reference, this object will appear in the GameObject inspector that the script has assigned, as we see in figure 9, this will allow us to manually assign the object we want to activate and deactivate, in our case the first object we had was called “GameObject”, in figure 10 you can see how the field has been filled with this object.

inspector of a gameobject, in the component of the script you can drag an object type gameobject
Fig. 9: In the inspector there is a field where we can place a GameObject type object.
inspector of a gameobject, in the component of the script an object type gameobject has been dragged
Fig. 10: We take the first object and drag it into the inspector’s field



Instruction to activate or deactivate a GameObject in Unity

The function that allows us to change the Activation status of a GameObject is “SetActive”, it is a method that receives as a parameter a boolean value (true or false) and this function is called on a GameObject, in this case we use the reference that we have defined, as seen in lines 22 and 27, we are deactivating and activating the GameObject respectively.

In this particular example we are reading the A and D keys to execute the functions, this means that if the player presses the A key, the instruction “SetActive(false)” is executed which disables the GameObject and if the player presses the D key, the instruction “SetActive(true)” is executed which enables the GameObject.

update function in unity, key reading, gameobject activation and deactivation using the set active function
Fig. 11: In the Update function we read the keyboard entries and carry out the activation and deactivation of the GameObject.

Executing those instructions is exactly equivalent to checking or unchecking that box in the inspector we saw in figures 2 and 3.



You have reach the end of the article, if it was useful consider subscribing to the channel!

Scroll to Top
Secured By miniOrange