I found that this worked fine in the Editor: Prior to quitting, the PlayerPref value would update correctly. (I verified this by looking at the corresponding registry key). However, in a build of the game, this code never succeeded at saving the value. The game would immediately quit, despite the call to PlayerPrefs.Save(), and the corresponding registry key would not update. (I’m aware that the registry keys for the PlayerPrefs values are different between running the game in-editor vs running a build.)
Anyway, after a little experimentation, I found that this approach would work properly if I turned it into a coroutine, and waited a single frame (yield return null) after calling PlayerPrefs.Save(). As long as I do that, it always saves properly before quitting.
What I don’t know is whether I can really count on this. I can imagine PlayerPrefs.Save() merely queuing up the actually saving to occur at the end of the frame, but I don’t know if that’s what’s really happening. My concern is that this is a timing issue, and that it works with a single frame delay on my system, but maybe some other system needs more time to properly save.
Does anyone else modify PlayerPrefs values just before quitting? Have you run into anything like this? And does anyone know if the single frame wait I’m doing should be reliable?
This may very well be a bug because the docs for PlayerPrefs.Save() actually mentions that it should run automatically when the application quits: Unity - Scripting API: PlayerPrefs.Save
So according to the docs, you shouldn’t have to call save at all. What happens if you remove the Save() call? Maybe it’s interfering with the automatic save that happens on application quits somehow?
Good call. It does seem that calling Save() actually causes it not to save. Removing the call to Save() causes the PlayerPrefs to save correctly. I’ll report it. Even though I shouldn’t have to call PlayerPrefs.Save() prior to quitting, it’s pretty bad that doing so seems to prevent saving from occurring at all.
I tried to reproduce this in a separate project, in order to report it, but (fortunately, I suppose) it works fine in the tiny project whether I call PlayerPrefs.Save() immediately prior to Application.Quit() or not. So, there may be something going on here, but I can’t narrow down what specifically causes it.
I’ll just omit the PlayerPrefs.Save() call in my project, and hope it doesn’t become an issue.
I get this problem in my project.
If I try write somethin in PlayerPrefs with timeScale=0 - saving didn’t work after Application.quit.
But if I try get value after saving before Quit - in my case just for Debug.Log(PlayerPrefs.GetString()) - saving work.
It work and without this debug, if i set timeScale=1;
Don’t know what actually it, but now I will checking save success aftere saving and before quit, I think.