- Home /
[C#] Save/Load Score stays at 0 even when changed
Been having this issue for a while now and it's giving me a headache. Here's the sum of it:
https://hastebin.com/xigezexiwa.cs (Save) https://hastebin.com/anogamunix.hs (Load) https://hastebin.com/otiquguvat.cs (Full Script)
So basically I'm making a high score, the 'data.highscore' is always equal to 0. So the if statement in Save is always true. I'm trying to figure this out but anybody's help could and would be appreciated, good day ^.^!
I used some code from the Live Training Session on Data Persistence. Hope I can get this problem solved soon, it's annoying me and I can't think of another way to solve it.
Hello, I don't think anyone is going to be able to help you based on what you provided. If you could please provide your full scripts (including but not limited to: where you call your Save function, and where you call your Load function) We would be able to help you a lot easier.
Is Data class marked as Serializable? The code seems fine.
$$anonymous$$aking an edit with the full script
Also, just to note I'd rather not use PlayerPrefs for score since, as PlayerPrefs suggests, it's not a player's preference. It's a score.
I've looked through your full script and everything seems to be in order, based on that script, I cannot yell you whats wrong. What I'm going to suggest is going in and "Debug.Log" ing it to death and see where its going wrong.
A far fetched reason that isn't working might be that, first time you saved, you created a data file with value of 0 at the field, now every other time you try to save you keep create files with same name but windows, probably mac too, do change the name to hightierdata.dat(1) or something like that,not sure right now/ too bored to check. So when you load you load the initial file the one you created the first ever time you called the function. To check if this is the issue either go to the file folder and delete all existing hightierdata.dat files and try again. If this works let me know to tell you how to fix the code.
Answer by dvir9299 · Apr 03, 2017 at 06:53 AM
Hi ! im also using that way to save memory ! but you should use the playerprefs to save that score , because its just a number ,nothing over that . if u want keeping using that way , you sould send the all script to so we can see where exaclly is the problem
Answer by UnityCoach · Apr 03, 2017 at 09:16 AM
public int HighScore
{
get { return PlayerPrefs.GetInt ("HighScore", 0); }
set { PlayerPrefs.SetInt ("HighScore", value); }
}