Unity Basics
  • Welcome
  • Getting Started with Unity Software
  • Helpful Shortcuts
  • The Unity Software Interface
    • The Default Interface
    • The Windows (Tabs)
      • Hierarchy Window
      • Scene Window
      • Project Window
      • Inspector Window
      • Game Window
      • Console Window
    • Other Layouts
      • Create a Custom Layout
  • Create
    • Creating Game Objects
      • 3D Primitives
      • Creating Game Objects in the Editor
      • Spawning Objects
      • Unhiding/Hiding Objects During Gameplay
      • Parenting
    • Creating Assets
    • Creating Components and Scripts
      • Adding Components in the Inspector
      • Creating Custom Components and Scripts
    • Creating Prefabs
  • Select and Update
    • General Selection
    • Search and Focus
    • Updating Game Objects
      • Updating Game Objects in the Editor
      • Updating Prefabs
    • Updating Assets
    • Updating Components
      • Update Components in the Inspector
      • Accessing Components Through Scripts
  • Delete
    • Deleting Game Objects
      • Deleting and Disabling Objects in the Editor
      • Using the Destroy() Function
    • Deleting Components and Scripts
      • Disabling and Removing Components in the Editor
      • Disabling and Removing Components During Gameplay
  • Translate, Rotate, and Scale
    • Intro to Transforms
    • Handy Transform Shortcuts
    • Translate
      • Positioning Game Objects in the Editor
      • Updating Position Through Script Code
      • Using the Transform.Translate() Function
    • Rotate
      • Rotating Game Objects in the Editor
      • Updating Rotation Through Script Code
      • Using the Transform.Rotate() Function
    • Scale
      • Resizing/Scaling Objects in the Editor
      • Updating Scale Through Script Code
    • Controlling Speed
    • Common Issues with Transforms
  • Materials
    • Material Basics
      • Creating and Applying Materials
      • Accessing Materials Through Code
  • Physics
    • Physics Basics
    • Colliders and Triggers
      • Collider Component Overview
      • Accessing Colliders Through Scripts
      • Common Issues: Colliders and Triggers
    • Rigidbody Component
      • Rigidbody Component Overview
      • Accessing the Rigidbody Component Through Scripts
      • Common Issues: Rigidbody Components
    • Common Issues: Physics and Velocity
  • Interaction
    • Interaction Basics
    • Keyboard Controls
    • Mouse Controls
  • Coding Basics
    • Intro to Scripts
    • Variables
    • Conditionals (If / Then / Else)
    • Arrays
    • Loops
    • Functions
Powered by GitBook
On this page
  • Using SetActive()
  • Example Code
  • Example 1 (using a gameObject type):
  • Example 2 (using a variable of another type):
  • Checking If the Game Object is Active

Was this helpful?

  1. Create
  2. Creating Game Objects

Unhiding/Hiding Objects During Gameplay

Using SetActive()

Use the script below to make a game object visible and its components available:

this.gameObject.SetActive(true);

And unavailable:

this.gameObject.SetActive(false);

Swap "this" with the variable name for a gameObject type.

Note: "gameObject" is only needed if the variable used is of a different type.

Scripts cannot access components of objects that are not active.

Example Code

Examples of a game object called "thing" that appears when "F" is pressed and disappears when "X" is pressed:

Example 1 (using a gameObject type):

using UnityEngine;

public class AppearDisappear : MonoBehaviour
{
    public GameObject thing;
    
    void Update
    {
        if (Input.GetKeyDown(KeyCode.F) {
            thing.SetActive(true);
        }
        
        if (Input.GetKeyDown(KeyCode.X) {
            thing.SetActive(false);
        }
    }
}

Example 2 (using a variable of another type):

using UnityEngine;

public class AppearDisappear : MonoBehaviour
{
    public Transform thing;
    
    void Update
    {
        if (Input.GetKeyDown(KeyCode.F) {
            thing.gameObject.SetActive(true);
        }
        
        if (Input.GetKeyDown(KeyCode.X) {
            thing.gameObject.SetActive(false);
        }
    }
}

REMEMBER: Hiding a parent object will hide its children.

Checking If the Game Object is Active

This code can help if you want things to behave differently if an object is visible or not.

this.gameObject.activeSelf

It is read-only (you cannot directly change this value) and will return true if active and false if not.

In the Hierarchy window, hidden / inactive objects will appear grayed out:

In the Inspector window, the checkbox next to the object's name controls the active state of the object. Example of an inactive object:

REMEMBER: Add the script to a game object in the scene. It will not work if it does not exist in the game.

PreviousSpawning ObjectsNextParenting

Last updated 4 years ago

Was this helpful?

Updating Game Objects
Deleting Game Objects