How Can I save game If user tries to forcefully close my game for example from task manager

When I play game if something bad happens I try close from task manager but game saves itself how can I do that . I use a static method for saving game I call it If I want to save

That’s a tricky one, but fear not, for I have just the solution for you!

    void OnApplicationQuit()

Here the SaveGame method, that’s being called contains your code to save the game. OnApplicationQuit() should be called when the game is closed, even when the player forcefully closes it - if it’s not frozen that is.

1 Like

One caveat: if the user hits "End Task" in the Task Manager, Windows kindly asks the program to quit. I'm not sure if it's identical to closing the window or hitting Alt-F4, but its' the same idea. You should be able to respond to that.

If the use hits "End Process", Windows immediately kills the game. There is no way to respond to this (which is why it works even if the program is frozen).

1 Like

Does this script have to be attached to an object that on the scene .

Well… yeah. Preferrably to one that will always be in the scene during the game, you know, you wouldn’t wanna have the GameObject that saves your game run out of health or anything.

1 Like

You can pretty easily do this without something needing to live in a scene by using Application.quitting:

what is difference between onapplicationquit and application quitting

Do I need to answer that?

One is a unity message, the other is a static delegate.

Just read the documentation.

1 Like

If your player is using the task manager to shut the game down, then they probably don't want to save anyway.

Still there might be good reason to. Saving in OnApplicationQuit() may for instance create a temporary savegame and ask the user if he wants to restore the previous game state, once he comes back, just in case he just forgot to save or couldn’t for some other reason. The player could still discard the option.

Then on the reason of why a player wouldn’t want to save: In older less sophisticated online multiplayer games, where the host was also one of the players, forcefully closing the application shortly before losing a game, was abused as a way to avoid messing up the win-loss-stats. That’s a scenario in which a player really shouldn’t be given the choice.

the player may also force-quit if the player gets stuck in a wall, falls out of bounds, or some other non-winnable, non-continueable scenario. So if you do save, then make sure the player can restore to a stable condition.


Reviving I know, but this is the top answer that comes up in google. Just for anyone stumbling on this thread, as you see above OnApplicationQuit may not always be called in cases where the game is forced closed.

The easiest way around this would be to save in appropriate intervals.. It's not usually saving exactly when you quit, but it's a nice backup to have rather than for example restarting a level.

Autosave is quite popular, strange was not mentioned here. Now it is. :)


While we're necroing old save game threads, keep in mind if you are trying to "jam in" a quick final save and something goes wrong like the OS decides, "Hey you're taking too long to SIGTERM, here have a nice little SIGKILL!" then you may simply end up with a corrupt that final save game.

This means if you want to go down this save-on-quit path you may also want to consider either separate final-quit-save areas or else implement rolling save game journaling in case the last one gets corrupt and you have to go to the previous one.