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().
public void StartNewGame()
{
SceneManager.LoadScene(newGameScene);
string saveGame = SceneManager.GetSceneByBuildIndex(1).name;
PlayerPrefs.SetString("SavedGame", saveGame);
GameManager.Instance.StartGame();
PlayerPrefs.SetInt("DayCount", 0);
Debug.Log(saveGame);
}
This second block is where I make day equal to the PlayerPref.GetInt()
private void Awake()
{
if (Instance == null)
Instance = this;
else if (Instance != this)
Destroy(this);
DontDestroyOnLoad(Instance.gameObject);
day = PlayerPrefs.GetInt("DayCount");
}
Lastly, this is where I make the day go up and save.
public void NewDay()
{
inDay = true;
day++;
PlayerPrefs.Save();
List<Action> events = new List<Action>
{
NormalDay,
};
Random randomEvent = new Random();
int selectedEvent = randomEvent.Next(0, events.Count);
events[selectedEvent].Invoke();
}
Not too sure what’s exactly going wrong, I’ve tried flipping and mixing things, but no luck.
It doesn’t work, because you are assigning prefs value only once - call PlayerPrefs.SetInt("DayCount", day);
whenever you change day’s value
Thanks for the quick response! I’ll give that a shot when I get the chance.
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")
pixaware_pwedrowski:
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")
public void NewDay()
{
inDay = true;
day++;
PlayerPrefs.GetInt("DayCount", day);
PlayerPrefs.Save();
}
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.
Instead of PlayerPrefs.GetInt("DayCount", day)
you probably want PlayerPrefs.SetInt("DayCount", day)
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)
pixaware_pwedrowski:
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)
StartNewGame() is supposed to reset the days. I have a continue game where it gathers the saved days. Thanks for the help!
1 Like
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:
Useful for a relatively small number of simple values.
2 Likes