What does gameObject.setActive does behind the scenes?

Hi everyone, currently I’m taking the Unity Turn Based strategy course, section Actions and UI
In one of the lectures we write the ActionBusyUI script, and there we are subscribing to the event on the UnitActionSystem class so we are listening whenever the value of isBusy changes so we know if we need to show or hide the ActionBusyUI but… since the game object is disabled how is the script still running?
I had issues in the past with this kind of behavior, whenever I disabled a game object my code inside the Update method no longer worked.

So that made me wonder, what exactly are we disabling when using gameObject.setActive(false)?
Is it just the MonoBehavior functions? Just the Update functions?

1 Like

Welcome back. :slight_smile:

Each GameObject has an activeSelf boolean state that can either be true or false. This is what setActive() is modifying.

When it is set to false, we won’t use any of its components. That includes colliders, renderers, scripts, etc. As you’ve noticed, that means the Update() method won’t be called on any scripts attached to the GameObject.

If we want more granular control over what we enable/disable then we can set the enabled state on each component. E.g. this.gameObject.GetComponent<MeshRenderer>().enabled = false;

It’s worth noting that even when we call setActive(false) on a GameObject, its components retain their enabled state. For example:

    void Start()

This code will print true, even though we have disabled the GameObject. Its components are still technically “enabled”, but they will not be called as the GameObjectitself is disabled.

I’m not sure what’s going on with the UnitActionSystem example as I’m not on that course, but hope that answers the question!


Yes, it’s a lot clearer now!

Thank you! :slight_smile:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms