Unity Programming Essentials: enabled vs active
In this post we explore two of the core concepts in Unity scripting. Enabled state of Unity Components (MonoBehaviours are example Components and we mainly refer to them when talking about Components) and Active state of Unity GameObjects. These concepts haunt pro Unity coders as well as beginners. Even after years of writing Unity code, I still run into problems related to these states. Let’s jump in!
GameObjects vs Components
It is important to understand the architecture of a Unity scene, specifically GameObjects and Components.
Blue: GameObject hierarchy in the scene.
Red: A Component on the “ParentCube” GameObject
Yellow: Set active state of GameObject
Green: Set enabled state of Components
GameObjects populate a Unity scene and live in a hierarchies. Each GameObject has one or more Components
attached to it. The hierarchy window shows the GameObjects in the current scene in their hierarchy. The Inspector window for a GameObject shows all the components that are attached to it.
A GameObject has a Transform component at least. You can add other components to it. When we code MonoBehaviours in Unity, we are creating new Components that we can add to our GameObjects later.
Active State
active
is the state of a GameObject and can be toggled through the top checkbox in the inspector window or code. The method to set the active state is GameObject.SetActive(bool)
.
A big source of confusion here is that when we talk about an active object, we almost always mean activeInHierarchy.
A GameObject is activeInHierarchy only if itself and all its parents are active.
You can check the active state of GameObject using 2 methods.
ActiveInHierarchy
is the actual property that tells us whether the GameObject is active.ActiveSelf
is the property that gets set through the Inspector UI or a script call toSetActive
.
The table below shows how these properties relate.
Enabled State
enabled
is the state of a component and can be toggled through the Inspector window or code. If a component is enabled
then it will have its Update
and Start
callbacks called. Note that other callbacks like OnTriggerEnter
will get called even for a disabled
component.
A Component is
enabled
if the GameObject it belongs to isactive
and it isenabled
.
We can check the enabled state of a Component using 2 properties.
enabled
property is the actual property that can be set using Inspector UI or code.IsActiveAndEnabled
is a derived property and actually tells us whether the Component is enabled.
mono.IsActiveAndEnabled
effectively equals tomono.enabled && mono.gameObject.IsActiveInHierarchy
Here is the full table showing how the states relate to each other. We can set the enabled
state of components and active
state of GameObjects.
So whats the big deal?
Note that ActiveInHierarchy
and ActiveAndEnabled
are more recent properties which cleared things up. In 2018 and older you had to check for enabled and active using your own helper methods.
Part of the confusion about these comes from the documentation. When we talk about enabled components, we usually mean ActiveAndEnabled.
For example the official Unity documentation for MonoBehaviour.Update says: “Update is called every frame, if the MonoBehaviour is enabled.” Which is true for ActiveAndEnabled components.
Also have in mind that during events that affect these properties, they might be inconsistent. In a recent bug in Unity 2019.3, in a Coroutine I faced a state where for a MonoBehaviour’s ActiveAndEnabled was true while ActiveInHierarchy was false!