For various reasons when OnDisable() is called I need to know whether it was triggered by:
a) the GameObject it is a component of having SetActive( false ) called on it, or…
b) …whether SetActive( false ) was called on a GameObject higher up the hierarchy
is it possible to tell, and how can you do it?
N.B. I couldn’t find any questions that asked this, and since I have now solved this problem I thought I would share the information with the community by asking the question and answering it myself.
The only way you could do this is by wrapping up the function SetActive within the class itself. There, you could set a flag in the wrapper, which tells the program “I am being called from within this object”, that you would reset at the end of the OnDisable method. That way, if the flag hasn’t been set, then it is being called from elsewhere, while if it is set, then it is being called from within the current script.
This would do it too, but in my use case I can just use the Unity flags :)
Your tests will probably prove to work well, my method is more general use, and is guaranteed to work as expected (using Unity's flags may produce problems if they do odd things, as you can't see the source code), but may be less readable or efficient than using Unity's flags, depending on how you use them.
Excellent, glad you sorted this out. Nice to see someone on here who is willing to continue testing and working out their own answers, rather than just waiting for someone else to do it.
This would do it too, but in my use case I can just use the Unity flags :)
– darbotronAh I see, I wasn't sure whether those would work for your case, but if they do, then that's great.
– HoeloeYour way is more bulletproof and generic, have chosen it as the best answer :)
– darbotronYour tests will probably prove to work well, my method is more general use, and is guaranteed to work as expected (using Unity's flags may produce problems if they do odd things, as you can't see the source code), but may be less readable or efficient than using Unity's flags, depending on how you use them.
– HoeloeExcellent, glad you sorted this out. Nice to see someone on here who is willing to continue testing and working out their own answers, rather than just waiting for someone else to do it.
– Hoeloe