How to have a script run at all times (even across scenes)

I am trying to make a script that preforms some cleanup when the game is exited, however when ever I load a new scene, I know that all object in it are destroyed, so I am wondering how to do this?

I have heard of a method (forgot name) that can load scenes without destroying the current scenes objects, but it leaved all objects behind, but I only want to leave behind one object

Hi,

What you may want to look at is DontDestroyOnLoad. it only affect the object you have declared as such, not all objects in the scenes.

For this, I typically create an empty game object called "GAMEMANAGER" or something, and make that object persists and old all the info and static scripts that needs to persists as levels are loaded/unloaded.

the other way if you actually only need clean up when application quit is to use the hook your clean up with Application.CancelQuit It has some limitation but could be a good solution.

Also, depending on what kind of clean up you need to do, you could implement it in each behavior script in need of clean up with OnApplicationQuit function

then when thew application quit you perform locally all the required clean up, and its elegant since it's within the behavior, so no global manager or anything like that.

Hope it helps,

Jean