When it is destroyed and the game continues

ondestroy will be executed when the scene is switched and when the game ends. I only want to execute it when the scene continues. Is there any good way?

Usually you set a flag for when the game is running or not, then decide to act based on that flag.

You can also consider using OnDisable() instead of OnDestroy()

1 Like
public static bool GameOverDude = false;  //set to true when the game is over

void OnDestroy()
{
    if (GameOverDude)
    {
        //Do whatever you want this script to do when destroyed and game is over
    }
    else
    {
        //Do whatever you want this script to do when destroyed but game continues
    }
}
1 Like

I try to modify OnGameDude in OnApplicationQuit()
But sometimes OnApplicationQuit will be executed later than OnDestroy
How to determine if the player has quit the game?

It seems that OnApplicationQuit does not work when the object is inactive

If the object belongs to DontDestroyOnLoad, then the switching scene has nothing to do with it, but if it happens to be destroyed when the scene is switched, according to the default practice, it will mistakenly think that it is the destruction caused by the scene switching
The situation of destruction is very complicated. It would be great if the engine could provide more state.