Playerprefs not working

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 :stuck_out_tongue:

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 !

Please show the code that you are using, using Code tags. How are you debugging? Tips for new Unity users

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 !

    void Start () {
            energyNow = PlayerPrefs.GetFloat ("Character_BarEnergy");
    }

    void OnApplicationPause (bool paused){

        if (paused) {
           PlayerPrefs.SetFloat ("Character_BarEnergy", energyNow);
           PlayerPrefs.Save();
        }
        if (!paused) {
            energyNow = PlayerPrefs.GetFloat ("Character_BarEnergy");
        }
    }

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.

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.

That’s possible. You could send a special debug build to one of the affected users, or obtain one of the devices.

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 ?!

//Store the current time when it starts
        currentDate = System.DateTime.Now;
        //Grab the old time from the player prefs as a long
        long temp = Convert.ToInt64(PlayerPrefs.GetString("sysString"));
        //Convert the old time from binary to a DataTime variable
        DateTime oldDate = DateTime.FromBinary(temp);

        //Use the Subtract method and store the result as a timespan variable
        TimeSpan difference = currentDate.Subtract(oldDate);

timeDiff = (float)difference.TotalSeconds;

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.

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:

 long temp = Convert.ToInt64(PlayerPrefs.GetString("sysString"));
//Convert the old time from binary to a DataTime variable
DateTime oldDate = DateTime.FromBinary(temp);

Found the issue.

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