Application.Quit and OnDestroy

Hey there,

I have a Problem with OnDestroy. So I have boxes which spawn loot which is triggered in the box script via OnDestroy. The loot when being spawn Registers itself to a gridfield on which it has been spawned.

If I Close the application / Stop the Editor Player, I get tons of exception Errors.

The Problem seems to be that when I Close the application, OnDestroy is being triggered which tries to spawn loot which cannot be registered to a gridfield since These also have been destroyed via quitting the application.

Is there any way to check if an application has been quit or not ?

Something like If(!Application.Quit){ do stuff…}

You’re probably familiar with the basic MonoBehaviour hooks: Update, Start, Awake, and so on.

There are quite a few others. One of them is OnApplicationQuit. You could use that function to set a flag in your script:

bool quitting;

void OnApplicationQuit() {
    quitting = true;
}

void OnDestroy() {
    if (quitting) {
        //application is quitting; don't spawn more stuff
    } else {
        //application is running, proceed normally
    }
}

I would love to know if there’s an easier way to check that, but at least it’s possible.