ACTIVATE and DEACTIVATE GameObjects through CODE in Unity

Introduction – Why activate and deactivate GameObjects?

GameObjects are the objects that can be placed in a scene in Unity, the simplest gameobject has at least a Transform component assigned, that allows to know its position, rotation and scale. You can add components to these GameObjects and give them a more specific behavior. For example, a Camera in Unity is an empty GameObject to which we add the Camera component and the AudioListener component.

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.

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



All the IMPORTANT information is summarized in the following TUTORIAL FROM MY YOUTUBE CHANNEL


How to manually activate and deactivate objects in Unity

As mentioned before, every GameObject consists of at least one Transform component. If we select the GameObject in the hierarchy, in the Inspector we will see this Transform component and also the header that is seen 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 on which the cursor is placed in both images is the game object state box, if this box is checked, the GameObject is active in the scene, if the box is unchecked, the GameObject is inactive in the scene and behaves as if it did not exist. In the hierarchy, an inactive object looks like Figure 4, in a more muted tone.

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.



I recently wrote an article about Components in Unity, sharing important things to know about them
CHECK THE ARTICLE HERE

Scroll to Top
Secured By miniOrange