The cover for Using the Unity Set Active Method

Using the Unity Set Active Method

Posted by Michael Sacco
./
August 31, 2023

Introduction

In Unity, you should be able to activate or deactivate game objects. When you deactivate a game object, you turn it off in the scene and turn off all components. Any children of that game object are also deactivated. Since you turned off all the components, they will never call Start, Update, or LateUpdate. In this guide, I’ll tell you everything you need to know about using Set Active in Unity.

Key Takeaways

  • Set the state of an object using GameObject.SetActive(bool state);
  • Get the state of an object using .activeSelf or .activeInHierarchy

How to use Set Active

You can set the state using the Set Active method. Using Set Active is easy. You don’t need to know the object’s current state to call SetActive. The state value should be your target state for the game object. True if you want it to be active, false if you want it to be inactive.

OnEnable and OnDisable

Unity will call OnEnable when an object becomes active. Unity will call OnDisable when an object becomes inactive. These two methods are basic event functions in Unity. You can use them to set up or dispose of data. Or, you can configure settings or get references.

Active Self vs Active In Hierarchy

Unity has two properties that can check whether a game object is active. You can inquire if a specific game object is inactive because you turned off that particular object. Alternatively, you can check if that object is off in general.

These two properties are .activeSelf and .ActiveInHierarchy.

Active Self

The active self property tells you if you have set a particular game object inactive. This property is more helpful if you want to check which specific object is the one that is causing the hierarchy stack to become disabled.

ActiveSelf only returns false if you directly toggle off that specific game object. The game object may be inactive due to a parent. However, the active self property will remain true if it is inactive in the hierarchy but has been set inactive indirectly by a parent.

Child vs. Parent

In Unity, a child belongs to a game object and a parent has a game object that belongs to it. You can use the GetComponentInParent method to find components in parent objects. Similarly, you can use GetChild method to find children of a parent.

Active in Hierarchy

The active in hierarchy property tells you if that game object happens to be inactive. It will always return false if the game object is inactive, including when you turn off that particular game object. This rule makes sense, but it’s good to know.

In general, you can use the active in hierarchy property to find if an object is enabled and active in your scene.

Both the active self and active in hierarchy properties could be more intuitive.

Is my game object disabled because of itself or because of a parent?

There’s a case that Unity doesn’t answer for you - is this object disabled because of itself or because of a parent?

I wrote a helper method that answers this question for you. Add this class to your project.


using UnityEngine;

public static class GameObjectHelpers

{
  public static bool IsDisabledByParent(GameObject gameObject)
  {
    if (gameObject.activeSelf && !gameObject.activeInHierarchy)
      return true;
    return false;
  }
}

This method returns true if the game object is active but deactivated because one of its parents is inactive.

Here’s how to use it:

using UnityEngine;

[ExecuteAlways]
public class GameObjectHelperTest : MonoBehaviour
{
  public GameObject target;
  
  void Update()
  {
    if (GameObjectHelpers.IsDisabledByParent(target))
    {
      Debug.Log(
        $"You didn't turn off {target.name}, but it was automatically disabled in the hierarchy by a parent. As a result, it is now inactive."
      );
    }
  }
}

Be careful

Suppose you have a component on an object and use SetActive to disable itself. In that case, you won’t be able to use that same component to re-activate it. That’s because this component is now disabled. Instead, try using a parent object to manage the child’s state. Or, turn off specific Components that you don’t need.

Developing an intuition

I wrote a simple script to help you develop an intuition for working with SetActive, activeSelf, and activeInHierarchy. This script logs the various properties to help you see how they work. To build this intuition, try activating and deactivating your game objects in the inspector and via script. Make sure to add your tracked game objects to the list in the inspector.

using System.Collections.Generic;

using UnityEngine;

[ExecuteAlways]

public class GetActiveState : MonoBehaviour

{
  public List<Transform> transformToWatch = new List<Transform>();
  
  void Update()
  {
    foreach (Transform t in transformToWatch)
    {
      if (t == null)
        continue;
      Debug.Log(
        $"{t.name} | Hierarchy State: {t.gameObject.activeInHierarchy}. Self State: {t.gameObject.activeSelf}"
      );
    }
  }
}

Conclusion

As a game developer, it’s crucial that you activate and deactivate game objects in your project. Being capable of managing a game object’s state is a beginner-level skill. If you’re learning other Unity basics, I encourage you to learn about OnValidate and LateUpdate, two of Unity’s essential event functions.

© 2024