I want to check only if the object has been disabled or set inactive, so I can prevent it from being disabled if a certain circumstance is true. I want to ignore when it is being destroyed to prevent errors. Something like this:
function OnDisable() {
if (!beenDestroyed && myStaticVariableIsTrue) {
print ("Disabled or Set inactive");
gameObject.active = true;
}
}
Problem is: I don’t have a valid beenDestroyed
and don’t know where to get one.
Main reason I want to do this (as I’ve explained in detail below) is because there are many scripts dealing with enabling and disabling (trough SetActiveRecursively
) with objects and only a few of those are to be “protected” in this way - coincidentally some of the top most children of them.
Preamble
Following a similar question, seems like this is how OnDisable
currently works, which is almost* (read below) useless:
function OnDisable() {
if (gameObject.active) {
print ("Disabled or Destroyed");
} else {
print ("Set inactive");
}
}
Possible Solutions
(A) OnDestroy
is only called after OnDisable
, so that doesn’t help. try
and catch
simply don’t work.
(B) If there’s no way to grab a value for beenDestroyed
we could remove it, which will bring up 3 lines of error per object:
(1) !IsDestroying()
UnityEngine.GameObject:set_active(Boolean)
TestOnDisable:OnDisable() (at Assets/TestOnDisable.cs:9)
Assert in file: Assets/TestOnDisable.cs: 9
(2) !gameObject.IsActive()
Assert in file: C:/BuildAgent/work/b0bcff80449a48aa/Runtime/Misc/GameObjectUtility.cpp at line: 853
(3) !m_IsActive
Assert in file: C:/BuildAgent/work/b0bcff80449a48aa/Runtime/BaseClasses/GameObject.cpp at line: 72
I could ignore the many, many lines due to many objects in scene. Because they don’t really matter in this case. The function is pretty much just that. But that would make it very difficult to use/read the Debug Console otherwise and I fear they might actually have some negative effect.
(C) DontDestroyOnLoad
probably might be used instead. But it would increase complexity in the code. A lot. I hate using it.
(D) And, of course, I could try and modify so many scripts to prevent disabling these specific objects to begin with… But using it this way would make much more sense programatically while keeping all the scripts much cleaner!
Appeal
Pretty please, any ideas?