Search Unity

Resolved Saving does not work

Discussion in 'Scripting' started by GroundstompStudios, May 22, 2022.

  1. GroundstompStudios

    GroundstompStudios

    Joined:
    Dec 25, 2016
    Posts:
    50
    Hey all, I wanna be able to save the day the player is currently on using PlayerPrefs, however it isn't working how it's supposed to.

    In the first block of code, is where I establish the PlayerPref.SetInt().
    Code (CSharp):
    1. public void StartNewGame()
    2.         {
    3.             SceneManager.LoadScene(newGameScene);
    4.  
    5.             string saveGame = SceneManager.GetSceneByBuildIndex(1).name;
    6.             PlayerPrefs.SetString("SavedGame", saveGame);
    7.            
    8.             GameManager.Instance.StartGame();
    9.             PlayerPrefs.SetInt("DayCount", 0);
    10.  
    11.             Debug.Log(saveGame);
    12.         }
    This second block is where I make day equal to the PlayerPref.GetInt()
    Code (CSharp):
    1. private void Awake()
    2.         {
    3.             if (Instance == null)
    4.                 Instance = this;
    5.             else if (Instance != this)
    6.                 Destroy(this);
    7.            
    8.             DontDestroyOnLoad(Instance.gameObject);
    9.            
    10.             day = PlayerPrefs.GetInt("DayCount");
    11.         }
    Lastly, this is where I make the day go up and save.
    Code (CSharp):
    1. public void NewDay()
    2.         {
    3.             inDay = true;
    4.             day++;
    5.             PlayerPrefs.Save();
    6.  
    7.             List<Action> events = new List<Action>
    8.             {
    9.                 NormalDay,
    10.             };
    11.  
    12.             Random randomEvent = new Random();
    13.  
    14.             int selectedEvent = randomEvent.Next(0, events.Count);
    15.             events[selectedEvent].Invoke();
    16.         }
    Not too sure what's exactly going wrong, I've tried flipping and mixing things, but no luck.
     
  2. pixaware_pwedrowski

    pixaware_pwedrowski

    Joined:
    Oct 25, 2018
    Posts:
    116
    It doesn't work, because you are assigning prefs value only once - call
    PlayerPrefs.SetInt("DayCount", day);
    whenever you change day's value
     
  3. GroundstompStudios

    GroundstompStudios

    Joined:
    Dec 25, 2016
    Posts:
    50
    Thanks for the quick response! I’ll give that a shot when I get the chance.
     
  4. pixaware_pwedrowski

    pixaware_pwedrowski

    Joined:
    Oct 25, 2018
    Posts:
    116
    No problem, just to give you a clue why it doesn't work - int is a value, not a reference type. So
    day = PlayerPrefs.GetInt("DayCount");
    gets value saved in prefs and sets it to
    day
    and any changes on
    day
    will not affect
    PlayerPrefs.GetInt("DayCount")
     
  5. GroundstompStudios

    GroundstompStudios

    Joined:
    Dec 25, 2016
    Posts:
    50
    Code (CSharp):
    1. public void NewDay()
    2.         {
    3.             inDay = true;
    4.             day++;
    5.             PlayerPrefs.GetInt("DayCount", day);
    6.             PlayerPrefs.Save();
    7.         }
    I assume like this? I swapped the days to 3, stopped playing in the editor, and hit play again and it reset it to 0.
     
  6. pixaware_pwedrowski

    pixaware_pwedrowski

    Joined:
    Oct 25, 2018
    Posts:
    116
    Instead of
    PlayerPrefs.GetInt("DayCount", day)
    you probably want
    PlayerPrefs.SetInt("DayCount", day)
     
    Last edited: May 22, 2022
  7. pixaware_pwedrowski

    pixaware_pwedrowski

    Joined:
    Oct 25, 2018
    Posts:
    116
    Btw if you want to save it between sessions, you need to change your
    StartNewGame()
    method as you'll always reset your DayCount pref to 0 with this line -
    PlayerPrefs.SetInt("DayCount", 0);
    . Remove it as
    GetInt
    will return 0 as a default value if the key doesn't exist. You can change default value by adding a parameter -
    PlayerPrefs.GetInt("DayCount", defaultIntValue)
     
  8. GroundstompStudios

    GroundstompStudios

    Joined:
    Dec 25, 2016
    Posts:
    50
    StartNewGame() is supposed to reset the days. I have a continue game where it gathers the saved days. Thanks for the help!
     
    pixaware_pwedrowski likes this.
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Rather than sprinkling your PlayerPrefs calls all over the place, which invites typos in the key names, I prefer this solution:

    Here's an example of simple persistent loading/saving values using PlayerPrefs:

    https://gist.github.com/kurtdekker/01da815d2dfd336a925ae38019c3a163

    Useful for a relatively small number of simple values.
     
    Bunny83 and pixaware_pwedrowski like this.