Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by jwlondon98 · May 11, 2015 at 07:34 AM · iosplayerprefsscorehighscore

Highscore PlayerPrefs not working on iOS

I am having a problem with the playerprefs i setup for my score/highscore system. Currently, the problem is that (on iOS) when the player dies, the highscore does not show. This problem does not occur in the editor on my computer when i run the game. It's strange.

Here's my code for where the score system occurs in the play screen. I believe the problem has something to do with the PlayerPrefs.Save(), considering I barely know how it works exactly. Keep in mind I am relatively knew to Unity and C#.

 void Update () 
     {
         //Score
         if (moving == true) {
             score += Time.deltaTime;                            //to reset highscore, set score = 0.01f
             scoreText.text = "" + score.ToString ("f2");
         }
 
         //Set Player Score Pref
         PlayerPrefs.SetFloat ("Player Score", score);
         PlayerPrefs.Save ();
         
         //Set High Score Pref
         highscore = PlayerPrefs.GetFloat ("High Score", score);    
         if (score > highscore) {                                //to reset highscore, switch 'highscore' with 'resetHS'
             PlayerPrefs.SetFloat ("High Score", score);
             PlayerPrefs.Save ();
         }
     }

In my Game Over screen I show the score and the highscore. The score works on iOS, but the highscore does not. Here is the code I used for that.

 void Start () 
     {
         PlayerPrefs.Save ();
         finalscore = PlayerPrefs.GetFloat ("Player Score");
         scoreText.text = "" + finalscore.ToString("f2");
         PlayerPrefs.Save ();
         highscore = PlayerPrefs.GetFloat ("High Score");
         highscoreText.text = highscore.ToString("f2");
     }

Thanks in advance for the help!

Comment
Add comment · Show 3
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 digzou · May 11, 2015 at 08:19 AM 0
Share

In line 14 of your first snippet.

highscore = PlayerPrefs.GetFloat ("High Score", score);

SHOULD BE

 highscore = PlayerPrefs.GetFloat ("High Score");

 

You cannot pass a vaue to a getter function. :)

avatar image taxvi · May 11, 2015 at 08:52 AM 0
Share

@digzou it's a default value that will be returned if PlayerPrefs had not been set. it's in the docs :| http://docs.unity3d.com/ScriptReference/PlayerPrefs.GetFloat.html

avatar image taxvi · May 11, 2015 at 09:02 AM 0
Share

I think it's these so many PlayerPrefs.Save (); calls that's causing the problem, I mean to call it every frame is too redundant, even worse calling it in FiexedUpdate (like x50 times per second). probably ios is simply not catching up with the disk read/write and maybe even skipping the operation or something (I'm just guessing, not an expert here). read this: http://docs.unity3d.com/ScriptReference/PlayerPrefs.Save.html

also it's not wise to call GetFloat in every frame either, call it once and save the value in a local variable.

0 Replies

· Add your reply
  • Sort: 

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

How to save a high score 2 Answers

Sorting Error 0 Answers

How can i get the following script back on track to being a single level high score saver 0 Answers

PlayerPref set int and get int 1 Answer

PlayerPrefs Highscore problem 1 Answer


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