I have a question if there is a way to detect in the OnDestroy() function if the object is destroyed because the level is closing/game is closing.
I have an object (soldier) that is spawning other objects in some situations (health-bar etc.). In the soldier's OnDestroy() I also remove all objects that the soldier spawned (soldier keeps references). The code looks like this:
When I close the editor I get the MissingReferenceException because Unity destroyed the m_CreatedObject before Soldier object when the game is closed. What can I do in this situation?
No, there isn't- but that's why the
callback exists! Use this to destroy the sub-elements before the game would close, preventing the OnDestroy problems from occuring.
answered Mar 06 '12 at 02:35 PM