Search Unity

PlayerPrefs.DeleteAll() - Weird Caching Issue

Discussion in 'Scripting' started by KOemxe, Jan 19, 2020.

  1. KOemxe

    KOemxe

    Joined:
    May 1, 2018
    Posts:
    35
    Hi everyone! I'm trying to reset ALL PlayerPrefs in this one function of my game to 1.) reset controls and 2.) reset unlocked vehicles. Here's my reset function:

    Code (CSharp):
    1. public void ConfirmReset()
    2.     {
    3.         PlayerPrefs.DeleteAll();
    4.         PlayerPrefs.Save();
    5.         SceneManager.LoadScene("RedirectionScene");
    6.     }
    And here's the logic checking for unlocked vehicles upon loading a scene:
    Code (CSharp):
    1. foreach(GameObject vehicle in globalVehicles)
    2.         {
    3.             Debug.Log("We have fetched " + vehicle.name + " to " + PlayerPrefs.GetString("unlocked_" + vehicle.name, "locked"));
    4.             if (PlayerPrefs.GetString("unlocked_" + vehicle.name, "locked") != "locked" && !unlockedVehicles.Contains(vehicle))
    5.                 unlockedVehicles.Add(vehicle);
    6.             else if (PlayerPrefs.GetString("unlocked_" + vehicle.name, "locked") != vehicle.name && unlockedVehicles.Count > 1)
    7.             {
    8.                 Debug.Log(vehicle.name + " should not be unlocked, removing");
    9.                 unlockedVehicles.Remove(vehicle);
    10.             }
    11.         }
    Now, here's the thing that's weird - I use a Debug.Log, and check to see if the player pref changed. It does see that. However, EVEN when I match the player pref to REMOVE a vehicle from the unlocked List, it still doesn't do it, even when the values match.

    Anyone else experienced this kind of problem? How do you deal with it?

    To clarify, this only happens during my current run of the game. When I run it again, it works fine, but mainly because of the vehicles not adding to the unlocked collection rather than previously unlocked vehicles getting removed.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    If you clear the playerprefs, then run that second method. The else if would run since the default value is locked and it wouldn't equal the vehicle name. Are you saying the second debug never shows?
     
  3. KOemxe

    KOemxe

    Joined:
    May 1, 2018
    Posts:
    35
    I'm saying that PlayerPrefs.GetString("unlocked_" + vehicle.name, "locked") is actually equal to "locked" in Debug.Log, but I can't actually match it to my string variable containing "locked" - PlayerPrefs.GetString("unlocked_" + vehicle.name, "locked") SHOULD equal the string variable containing "locked" but it does not.

    For what it's worth, the second method is from a static singleton type of object. I know there's been problems with that before.
     
  4. KOemxe

    KOemxe

    Joined:
    May 1, 2018
    Posts:
    35