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.

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.

Fig. 2: Activation checkbox of the GameObject, at this time it is active.
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.

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.

Fig. 5: We created a new script to make the activation and deactivation of the GameObject by code.
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.

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.

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.

Fig. 9: In the inspector there is a field where we can place a GameObject type object.
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.

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

Exit mobile version
Secured By miniOrange