1
\$\begingroup\$

I am looking over a script that has a particular function called to disable the script and mark a list for re-structuring:

void Disable()
{
    gameObject.SetActive(false);
    isEnumerated = false;
}

What I am trying to determine is whether it would be better to simply have the code in the OnDisable() function:

void OnDisable()
{
    isEnumerated = false;
}

I found that OnDisable() calls whenever the game object is "disabled" or "inactive", but I am a bit confused on the exact difference between the two. Documentation I have found that references both is only specific in that they are two different cases, where places that offer definition appear to use the two words synonymously.

\$\endgroup\$
  • 1
    \$\begingroup\$ AFAIK OnDisable() is the method used by Unity when disabling an object. Where is the Disable() script being called from? The Disable() method, by calling gameObject.SetActive(false), is causing that gameObject and its MonoBehaviours to be deactivated. \$\endgroup\$ – TJClifton Jun 7 '15 at 11:47
  • \$\begingroup\$ @TJClifton OnDisable() and Disable() are being called from the same script. Disable was the original function. I have re-written it as OnDisable, as Disable() is being called to deliberately disable the script, where I want to change the bool if the object (a GUI Texture) is not on the screen at all. \$\endgroup\$ – Gnemlock Jun 8 '15 at 0:42
  • \$\begingroup\$ Can you link the Documentation you found that specifies they are two different things? As far as I know they are just words used interchangeably to indicate a GameObject that has been "disabled" by either turning it off with SetActive(false), or being the child of an "inactive" or "disabled" parent... You can think of "disabled" as being the act of setting a gameobject to inactive. \$\endgroup\$ – Jeremiah Leslie Jun 8 '15 at 10:24
1
\$\begingroup\$

As far as I can tell, when an object is disabled it is "turned off" from the current scene: it doesn't perform behaviors, code, event or interactions with other instances. The only exception is responding to scripts that "enable" back disabled object. So, a disabled object is like it isn't there but there's a reference to it somewhere in order to enable it for use. This can be a better solution than having an object flagged as not active (in a naive way) but still existing and having its own CPU calculations, because you can enable objects as soon as you actually need them.

An inactive object is something similar to a disabled object, though it can still responds to global functions (you can find inactive objects, but not disabled ones) and so on.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.