- Home /
Cleanup when scene is unloaded
I'd like to have some object that will be called when a scene is unloaded, to perform certain cleanups.
Is there any deterministic way of running code when a scene unloads?
By deterministic, i mean - controlling exactly WHEN the cleanup code runs (e.g: before any of the other scene objects' OnDestroy, for example).
So far i have an object in the scene with a script that handles OnDestroy, but there's no guarantee to control when that code actually will get called.
Answer by MartinCA · Aug 09, 2014 at 06:57 PM
Why not just wrap your scene loading method with a manager that dispatches a message before calling LoadScene?
Something along these lines
Class SceneManager
{
public delegate void SceneLoadEvent();
public static event SceneLoadEvent OnBeforeSceneLoad;
public static void LoadScene( string sceneName )
{
if ( OnBeforeSceneLoad != null )
{
OnBeforeSceneLoad();
}
Application.LoadScene( sceneName );
}
}
Your answer
Follow this Question
Related Questions
Destroy + Score 1 Answer
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Inventory problems (C#) [edited to be more clear] 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers