Order of destroy objects and OnDestroy event

Hi.

I’m working on a game where some singletons are present between scenes. However, other singletons are different in each scene so I don’t use DontDestroyOnLoad method for these.

The problem I found is that the game object that contains the singleton is destroyed in an arbitrary order (checked by debug logs in the console). I can change the script execution order in the project settings but this seems not to work with OnDestroy and OnDisable events.


51091-orderunity2.png

The only thing I need is my singleton to be destroyed the last one. Is there a way to control the order of destroyed objects when a scene is loaded?

Thanks in advance!

Discovered the same issue. It’s a bummer OnDestroy doesn’t respect script execution order.

Couldn’t find a way to set the order. Instead, discovered a way to deal with not knowing the order. I ended up creating a public static bool called WasDestroyed inside of the singleton class to track OnDestroy. WasDestroyed’s value is set when the singleton calls its OnDestroy. Then I could check WasDestroyed in other scripts OnDestroy to see if I should be accessing the instance or not.

I encountered the same problem and if someone came across this topic I recommend to use a scene and to put your singletons inside, a scene manager.

Using SceneManager.LoadScene(, LoadSceneMode.Additive) and SceneManager.UnloadScene

Reference : Multi-Scene editing