Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by boatakks · Mar 23, 2016 at 11:26 AM · playerprefsscoreboardleaderboards

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 ();
         }




Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image boatakks · Mar 28, 2016 at 09:23 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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)
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image boatakks · Apr 01, 2016 at 05:00 PM 0
Share

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.

avatar image boatakks · Apr 01, 2016 at 05:18 PM 0
Share

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?

avatar image boatakks · Apr 05, 2016 at 04:49 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges