Search Unity

PlayerPrefs persist on fresh install.

Discussion in 'Editor & General Support' started by wesdaco, Aug 1, 2019.

  1. wesdaco

    wesdaco

    Joined:
    Aug 28, 2017
    Posts:
    5
    I'm running into a weird bug that loads a previously saved state in my Unity Editor when I do a fresh Android install.

    I'm currently using the new Unity 2019.2

    1. My game keeps track of 3 things: Coins, Stars and Levels Unlocked (connected to how many Stars you have).
    2. I played around in Unity and collected 200 coins, 37 stars and 2 levels unlocked.
    3. When I do a fresh install on an Android device - it starts with 0 coins, 0 stars and 0 levels unlocked.
    4. If I force close the app and restart it - it shows 200 coins, 37 stars and 2 levels unlocked.

    It's remembering my Unity saved state.

    What's even more odd is that if I play the game on Android for a bit and collect some coins and stars and then force restart it - it will remember how many coins I collected, but the stars will reset back to 37.

    5. I collect 350 coins, 55 stars and 3 levels unlocked then force close and restart - it shows 350 coins (correct), 37 stars and 2 levels.

    It has both my programmer and me stumped. Any initial thoughts?

    This is the code in my Game Manager that's related to the coins and stars.

    Code (CSharp):
    1. public void AddCoins(int coins)
    2.     {
    3.         this.coins += coins;
    4.     }
    5.  
    6.     public int coins
    7.     {
    8.         get
    9.         {
    10.             return PlayerPrefs.GetInt("coins", 0);
    11.         }
    12.         set
    13.         {
    14.             coinsAdded?.Invoke(value);
    15.             PlayerPrefs.SetInt("coins", value);
    16.         }
    17.     }
    18.  
    19.     public void AddImages(int images)
    20.     {
    21.         this.images += images;
    22.     }
    23.  
    24.     public int images
    25.     {
    26.         get
    27.         {
    28.             return PlayerPrefs.GetInt("images", 0);
    29.         }
    30.         set
    31.         {
    32.             unlockedImages?.Invoke(value);
    33.             PlayerPrefs.SetInt("images", value);
    34.         }
    35.     }
    36.  
    37.     public void Save()
    38.     {
    39.         levelCreator.Save();
    40.     }
    41.  
    42.     private void OnApplicationQuit()
    43.     {
    44.         Save();
    45.     }
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I don't know what you mean about PlayerPrefs in the editor when you do a fresh Android install. PlayerPrefs in the editor are stored in the Windows registry, or on Mac wherever they are normally stored there. PlayerPrefs on an Android device are not stored with the application.
    https://answers.unity.com/questions/131511/playerprefs-storage-location-on-android-or-ios.html

    As for sometimes not saving PlayerPrefs, where are you calling PlayerPrefs.Save()? I don't see it here.

    https://docs.unity3d.com/ScriptReference/PlayerPrefs.Save.html

    If you're not calling PlayerPrefs.Save() then it will automatically be called when the user exits your game, but if the game crashes or otherwise doesn't get a chance to call OnApplicationQuit then all progress up to there is lost.
     
  3. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
  4. wesdaco

    wesdaco

    Joined:
    Aug 28, 2017
    Posts:
    5
    Thanks, Jeff.

    I did a bit more digging and actually found that it's the PlayerPrefs that save correctly on a force stop, but the Application.persistentDataPath does not (my programmer used both in the game). Coins are set by PlayerPrefs and Stars set to Application.persistentDataPath (Application.persistentDataPath + "/json.data")

    Any ideas?

    I'm trying FORCE INTERNAL build to see if that fixes the problem as found in this old question - https://answers.unity.com/questions/1223929/android-data-save-issues-does-the-install-location.html
     
  5. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
  6. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    @JeffDUnity3D I'm fairly certain he just means that the Stars are saved to a data file at
    Application.persistentDataPath
    .

    @wesdaco Is there any code to save the stars on Application.Quit? It won't do it automatically for you like PlayerPrefs seems to.