Search Unity

Playerprefs not working

Discussion in 'Android' started by neufoctobre, Oct 29, 2019.

  1. neufoctobre

    neufoctobre

    Joined:
    Jun 20, 2015
    Posts:
    94
    Hi everyone !

    I'm very struggled with a problem I'm facing for 2 month now !
    I even don't know how to explain and where I should investigate. Thats why you are my last chance :p

    Everything worked fine for years but after unity 2019.2.3 the problem appeared. I only update the unity version, my scripts are the sames.

    The issue :
    Some players report me that their "Energy" progression are no saved anymore, when they start the game after an older session. On my test devices everything is fine.

    My idea :
    I have a energy bar on my game, I filled it with a small amount when the player get some energy or I remove some amount when the player get hurt. I'm using Playerprefs.GetFloat at start to initialize the bar and Playerprefs.SetFloat to save the progression. So I think the problem could be the getFloat are not working anymore.

    But as my script is same for years I'm not sure to investigate in the right way, does someone facing this issue or have an idea how I should investigate this ?

    thank you very much for reading !
     
  2. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
  3. neufoctobre

    neufoctobre

    Joined:
    Jun 20, 2015
    Posts:
    94
    Here is my code, use it for many years but didn't work now if the problem come from here

    I'm using android studio to analyse the logcat while debuging but this issue happen to many of my gamers, it's working on my device so I'm struggle o how I should debug it !

    Code (CSharp):
    1.  
    2.  
    3.     void Start () {
    4.             energyNow = PlayerPrefs.GetFloat ("Character_BarEnergy");
    5.     }
    6.  
    7.     void OnApplicationPause (bool paused){
    8.  
    9.         if (paused) {
    10.            PlayerPrefs.SetFloat ("Character_BarEnergy", energyNow);
    11.            PlayerPrefs.Save();
    12.         }
    13.         if (!paused) {
    14.             energyNow = PlayerPrefs.GetFloat ("Character_BarEnergy");
    15.         }
    16.     }
     
  4. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    You'll probably need to test on the same device(s) as your users. Keep in mind that on some Android devices, PlayerPrefs is deleted if you reinstall the app, so make sure to test that too.
     
  5. neufoctobre

    neufoctobre

    Joined:
    Jun 20, 2015
    Posts:
    94
    But the problem happen between 2 sessions not after a reinstall.
    I unfortunately can't test on the same device but I know the problem appear to :
    - Samsung J8
    - Motorola Moto G3

    Seems some playerprefs are not save or not read.
     
  6. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    That's possible. You could send a special debug build to one of the affected users, or obtain one of the devices.
     
  7. neufoctobre

    neufoctobre

    Joined:
    Jun 20, 2015
    Posts:
    94
    But its only some playerprefs who don't work. I had the chance to ask them about other mecanism using playerprefs and it work well.
    This bug drive me crazy.

    I had the problem after using app bundle then my last update I gave up with app bundle and just split apk.
    I though may be is it possible my game will find some playerprefs not at the good location ? or is there only one location for each app ?

    I'm also using this method to modifiy these playerprefs on game start :
    May be this function doesnt work on some device and result in a false number wich result to my playerprefs to be 0 ?!

    Code (CSharp):
    1. //Store the current time when it starts
    2.         currentDate = System.DateTime.Now;
    3.         //Grab the old time from the player prefs as a long
    4.         long temp = Convert.ToInt64(PlayerPrefs.GetString("sysString"));
    5.         //Convert the old time from binary to a DataTime variable
    6.         DateTime oldDate = DateTime.FromBinary(temp);
    7.  
    8.         //Use the Subtract method and store the result as a timespan variable
    9.         TimeSpan difference = currentDate.Subtract(oldDate);
    10.  
    11. timeDiff = (float)difference.TotalSeconds;
     
  8. neufoctobre

    neufoctobre

    Joined:
    Jun 20, 2015
    Posts:
    94
    No one have an idea ?
    I'm losing random player everyday complaining the energy bar don't work, I really don't know how to manage a problem randomly appear like this.
     
  9. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    I offered some suggestions, get the same device or send a debug build to one of the affected users. You are not checking the output when you call your methods. In the following case, you never check the value of temp before using it:

    Code (CSharp):
    1.  long temp = Convert.ToInt64(PlayerPrefs.GetString("sysString"));
    2. //Convert the old time from binary to a DataTime variable
    3. DateTime oldDate = DateTime.FromBinary(temp);
    4.  
     
  10. neufoctobre

    neufoctobre

    Joined:
    Jun 20, 2015
    Posts:
    94
    Found the issue.

    For some device, the "OnApplicationPause" is called before "Start" function.
    Is that normal ?