how do i save player prefs?
I am creating an extended version of the Space Shooter game. In this version i want an ingame leaderboard that comes with preset values. When you beat one of those scores, I want the leaderboard to permanently update with the new values even if the application is quit and restarted.
to do this, i have created a leader board script with a bool called once. I want once to stay true until a player adds a score to the leaderboard. I created a player pref int called "once" which starts set to 0. if that pref is 0 then the bool once is set to true and and an update function takes place that sets the player score to one of the leaderboard scores and the rest of the leaderboard score are set to their own playerpref ints, and playerpref int "once" is set to 1 so that the bool once will always read false from then on. this bit of code is in my start function
save = SaveScore.control.saveScore;
if (PlayerPrefs.GetInt ("once") == 0)
{
once =true;
}
if (PlayerPrefs.GetInt ("once") == 1)
{
once = false;
}
if (once = true)
{
if (save < 13000 && save > 7000)
{
PlayerPrefs.SetInt ("once", 1);
third = save;
tText.text = "3rd : " + third;
PlayerPrefs.SetInt ("3rd", third);
PlayerPrefs.SetInt ("2nd", 13000);
PlayerPrefs.SetInt ("1st", 20000);
}
if (save < 20000 && save > 13000)
{
PlayerPrefs.SetInt ("once", 1);
third = second;
tText.text = "3rd : " + third;
PlayerPrefs.SetInt ("3rd", third);
second = save;
sText.text = "2nd : " + second;
PlayerPrefs.SetInt ("2nd", second);
PlayerPrefs.SetInt ("1st", 20000);
}
if (save > 20000)
{
PlayerPrefs.SetInt ("once", 1);
third = second;
tText.text = "3rd : " + third;
PlayerPrefs.SetInt ("3rd", third);
second = first;
sText.text = "2nd : " + second;
PlayerPrefs.SetInt ("2nd", second);
first = save;
fText.text = "1st : " + first;
PlayerPrefs.SetInt ("1st", first);
}
PlayerPrefs.Save ();
}
If the bool once is false i also run this bit of code in my start function
if (once = false)
{
fText.text = "1st : " + PlayerPrefs.GetInt ("1st");
sText.text = "2nd : " + PlayerPrefs.GetInt ("2nd");
tText.text = "3rd : " + PlayerPrefs.GetInt ("3rd");
if (save < PlayerPrefs.GetInt ("2nd") && save > PlayerPrefs.GetInt ("3rd"))
{
PlayerPrefs.SetInt ("once", 1);
third = save;
tText.text = "3rd : " + third;
PlayerPrefs.SetInt ("3rd", third);
}
if (save < PlayerPrefs.GetInt ("1st") && save > PlayerPrefs.GetInt ("2nd"))
{
PlayerPrefs.SetInt ("once", 1);
third = second;
tText.text = "3rd : " + third;
PlayerPrefs.SetInt ("3rd", third);
second = save;
sText.text = "2nd : " + second;
PlayerPrefs.SetInt ("2nd", second);
}
if (save > PlayerPrefs.GetInt ("First"))
{
PlayerPrefs.SetInt ("once", 1);
third = second;
tText.text = "3rd : " + third;
PlayerPrefs.SetInt ("3rd", third);
second = first;
sText.text = "2nd : " + second;
PlayerPrefs.SetInt ("2nd", second);
first = save;
fText.text = "1st : " + first;
PlayerPrefs.SetInt ("1st", first);
}
PlayerPrefs.Save ();
}
but this script is not working to write the player prefs to disk. the leaderboard sets to the default values no matter what. How do i make this function properly?
the full start function looks like this
save = SaveScore.control.saveScore;
if (PlayerPrefs.GetInt ("once") == 0)
{
once =true;
}
if (PlayerPrefs.GetInt ("once") == 1)
{
once = false;
}
if (once = true)
{
if (save < 13000 && save > 7000)
{
PlayerPrefs.SetInt ("once", 1);
third = save;
tText.text = "3rd : " + third;
PlayerPrefs.SetInt ("3rd", third);
PlayerPrefs.SetInt ("2nd", 13000);
PlayerPrefs.SetInt ("1st", 20000);
}
if (save < 20000 && save > 13000)
{
PlayerPrefs.SetInt ("once", 1);
third = second;
tText.text = "3rd : " + third;
PlayerPrefs.SetInt ("3rd", third);
second = save;
sText.text = "2nd : " + second;
PlayerPrefs.SetInt ("2nd", second);
PlayerPrefs.SetInt ("1st", 20000);
}
if (save > 20000)
{
PlayerPrefs.SetInt ("once", 1);
third = second;
tText.text = "3rd : " + third;
PlayerPrefs.SetInt ("3rd", third);
second = first;
sText.text = "2nd : " + second;
PlayerPrefs.SetInt ("2nd", second);
first = save;
fText.text = "1st : " + first;
PlayerPrefs.SetInt ("1st", first);
}
PlayerPrefs.Save ();
}
if (once = false)
{
fText.text = "1st : " + PlayerPrefs.GetInt ("1st");
sText.text = "2nd : " + PlayerPrefs.GetInt ("2nd");
tText.text = "3rd : " + PlayerPrefs.GetInt ("3rd");
if (save < PlayerPrefs.GetInt ("2nd") && save > PlayerPrefs.GetInt ("3rd"))
{
PlayerPrefs.SetInt ("once", 1);
third = save;
tText.text = "3rd : " + third;
PlayerPrefs.SetInt ("3rd", third);
}
if (save < PlayerPrefs.GetInt ("1st") && save > PlayerPrefs.GetInt ("2nd"))
{
PlayerPrefs.SetInt ("once", 1);
third = second;
tText.text = "3rd : " + third;
PlayerPrefs.SetInt ("3rd", third);
second = save;
sText.text = "2nd : " + second;
PlayerPrefs.SetInt ("2nd", second);
}
if (save > PlayerPrefs.GetInt ("First"))
{
PlayerPrefs.SetInt ("once", 1);
third = second;
tText.text = "3rd : " + third;
PlayerPrefs.SetInt ("3rd", third);
second = first;
sText.text = "2nd : " + second;
PlayerPrefs.SetInt ("2nd", second);
first = save;
fText.text = "1st : " + first;
PlayerPrefs.SetInt ("1st", first);
}
PlayerPrefs.Save ();
}
I am VERY new to scripting. I only started learning C# about two months ago. I would really like some help to fix this, as it will $$anonymous$$ch me some of the necessary skills to make my own more complex game. Saving playerprefs will be absolutely necessary for the game I have in $$anonymous$$d.
I look at the script i created, and I dont see why it wont LOGICALLY work. Is the problem with my bool? Originally i just had the if statement check the playerpref "once" was set to 1 or not, but that didnt seem to work so i set to a bool that was relative to "once". Should i wrap my code up in a function and then call that function in start?
I seriously dont understand why this isnt working, I have spent the last five days combing the Answers forum and I havent found a solution that works for me. In fact I have found answers that confirm that my script SHOULD work.
Someone throw me a life preserver please.
Answer by jgodfrey · Mar 29, 2016 at 12:56 AM
This...
if (once = true)
and
if (once = false)
should be...
if (once == true)
and
if (once == false)
Thanks so much man. The console was not showing any errors so I didnt understand that this needed to be changed. Like I said , i am very new to scripting. I really appreciate the correction.
i made a quick change to the script and your solution worked fine. But now when I build the game, the player prefs that got saved in the editor are persisting into the built version of the game. I dont want this to happen. I want the built version to start fresh with the defaults. Do you know why this is happening?
I added a line to my script to delete the playerprefs that i run once before the game is built. now my leaderboard script is working perfectly. Thank You for fixing my newb mistake.
Your answer

Follow this Question
Related Questions
SOLVED : How to scan and delete elements from a name/score array on a dreamlo database 2 Answers
Faster Way to Increment PlayerPrefs Int 1 Answer
Playerprefs, i dont know how to use 0 Answers
Editor PlayerPrefs getting repopulated with deleted values 2 Answers
standard anti memory hack load save data using playerprefs 0 Answers