PlayerPrefs is saved bfore calling that line of code

I have a gamemanager script with this bit of code

    public void Savegame()
    {
        PlayerPrefs.SetString ("SavegameExists", "yes");
        print ("gameSaved");
    }

    void Awake ()
    {
        if PlayerPrefs.GetString ("SavegameExists") == yes)
            print("savegameExits");
    }
}

The problem is that even though I never call the event Savegame(), the PlayerPrefs string is set to yes. Using “if PlayerPrefs.HasKey” doesn’t work either. So how can I check if a savegame exist upon start and if yes, call a LoadGame function?

Thank you!

PlayerPrefs is just a database of keys that map to pieces of data, in the case above, keys that map to strings. If you change it and then check it, it will have changed.

PlayerPrefs.SaveGame() is automatically called when your app exits normally, or you can call it on demand to write the current collection of PlayerPrefs keys/data to the storage area for your app, either disk (on PC) or application data (on mobile for instance).

All further logic about whether a savegame actually exists or not is up to you to implement. If you need to serialize more complicated structures, to PlayerPrefs, you can do so one field at a time but it is highly tedious and error prone. Instead I recommend you look into something like JSON serialization, which Unity supports directly, and use that to turn your data block into a single string that can be written to PlayerPrefs, then read back.

But again, it’s entirely up to you: PlayerPrefs is just a bag of data, and SaveDisc() only writes it to permanent storage, which WILL happen anyway. You have to implement the actual “I have saved the state of my gameplay” logic yourself.

1 Like

EDIT:
Kinda got it working now, what was confusing me was that the editor saved the PlayerPrefs between test runs and apparently then took that value into the WebGL build. By making a fresh key or deleting it I got it working the way I want it!
/EDIT

Thanks for your answer but I’m still confused. When I start the game it already takes the value that WILL BE assigned when the Savegame function is called, but I never called it. So far I’ve been using Save and Load quite easily, but with buttons that call those functions and override either the PlayerPrefs (save) or the actual variables (load). But I want the game to automatically check upon start if the game has ever been saved before.
That seems impossible, because even the very first time the game is run the PlayerPrefs takes whatever value will be assigned in the SaveGame function.

To check for a first run, you can always query for a particular key and see if it is nonzero:

 if (PlayerPrefs.GetInt( "GameHasBeenRunBefore", 0) > 0)
 {
  Debug.Log( "FIRST RUN!");
  // do other stuff here for first run
 }
 // now mark down that we HAVE run before
 PlayerPrefs.SetInt( "GameHasBeenRunBefore", 1);

The second argument I supplied to GetInt is the default value. It is optional but why not include it to be very specific and avoid future confusion.

Also I recommend assigning those key strings to a single const string to avoid typos, as they are case sensitive and must be precisely typed.

 const string s_GameHasBeenRunBefore = "GameHasBeenRunBefore";