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.
In the following video we see how to ACTIVATE and DEACTIVATE GameObjects in Unity through code every certain amount of time.
This is achieved by executing the “SetActive” function sending “true” or “false” to the GameObject you want to activate and deactivate.
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.
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.
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.
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.
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.
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.
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.
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