Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

PlayerPrefs Not Recalling

Discussion in 'Editor & General Support' started by renman3000, Aug 28, 2020.

  1. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    Is there any logical reason this code, if it is in fact being called, should not save?


    Code (csharp):
    1.  
    2.     public void getThisWorldProgress()
    3.     {
    4.         //via gamemng
    5.         crntWorldAsInt = PlayerPrefs.GetInt("crntWorld", 0);
    6.         crntLevelAsInt = PlayerPrefs.GetInt("crntLevel", 0);
    7.      
    8.        
    9.     }
    10.  
    11.     public void saveData()
    12.     {
    13.  
    14.         PlayerPrefs.SetInt("crntWorld", crntWorldAsInt);
    15.         PlayerPrefs.SetInt("crntLevel", crntLevelAsInt);
    16.         PlayerPrefs.Save();
    17.  
    18.  
    19.  
    20.     }
    21.  
    22.  
    23.  
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,697
    Looks like it should work, assuming it's being called, as you said. I'd suggest creating string constants to rule out any spelling errors, e.g.:
    Code (CSharp):
    1.     const string crntLevelKey = "crntLevel";
    2.     const string crntWorldKey = "crntWorld";
    3.  
    4.     public void getThisWorldProgress()
    5.     {
    6.         //via gamemng
    7.         crntWorldAsInt = PlayerPrefs.GetInt(crntWorldKey, 0);
    8.         crntLevelAsInt = PlayerPrefs.GetInt(crntLevelKey, 0);
    9.     }
    10.  
    11.     public void saveData()
    12.     {
    13.         PlayerPrefs.SetInt(crntWorldKey, crntWorldAsInt);
    14.         PlayerPrefs.SetInt(crntLevelKey, crntLevelAsInt);
    15.         PlayerPrefs.Save();
    16.     }
     
  3. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680

    Hi thanks.
    Unfortunately there is no difference. ???
    Pic is my print to console pasted is code.
    ??
    Ugh
    Code (csharp):
    1.  
    2.     public void getThisWorldProgress()
    3.     {
    4.         //via gamemng
    5.         crntWorldAsInt = PlayerPrefs.GetInt(crntWorldKey, 0);
    6.         crntLevelAsInt = PlayerPrefs.GetInt(crntLevelKey, 0);
    7.         print("UWLP: SD: ==========>  getData() w:" + crntWorldAsInt + ", l:" + crntLevelAsInt)
    8.     }
    9.     public void saveData(){
    10.         PlayerPrefs.SetInt(crntWorldKey, crntWorldAsInt);
    11.         PlayerPrefs.SetInt(crntLevelKey, crntLevelAsInt);
    12.         PlayerPrefs.Save();
    13.         print("UWLP: SD: saveData() w:" + crntWorldAsInt + ", l:" + crntLevelAsInt);
    14.     }
    15.  
    16.  




    The cycle in pic sees getData (0,0).... levelCleared, then saveData (0,1).... then reload level where getData(0,0)
    !!
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,697
    Are you calling PlayerPrefs functions in any other scripts in your game?
     
  5. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680

    No that I am aware of.
    This is a method I have used before... Looking at using alternatives, like Scriptable Objects
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,697
    Scriptableobjects won't help you. They don't save data between runs.
     
  7. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680

    Hmm.