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 utAlpha09 · Mar 04, 2013 at 01:23 AM · playerprefshighscore

Need Help Scripting a Highscore System

Hi,

I am currently working on a game that requires storing highscores for the user, that will display at the beginning of the scene and save over the previous time if the new one is better. I am trying to use player preferences, but I am having little luck. In a nutshell, this code is not saving the values to the player preferences, and i don't know why. Is it something obvious or is there something that i am forgetting.Here is my current code:

 var myTimer: float=2.0;
 var GameCollision : String;
 var myTime : float=2.0;
 var reciver : int;
 static var score : int = 0;
 var highscore : int = 0; 
 
 function Update ()
 {
     if( reciver==0 ) {
     
     Highscore = PlayerPrefs.GetInt("High Score");
     guiText.text = highscore.ToString();
     myTimer+=Time.deltaTime;
     WaitForSeconds(5);
     guiText.text = myTimer.ToString();
     }
     
     
 }
 
 function OnCollisionEnter (myCollision : Collision) {
     if(myCollision.gameObject.name == (GameCollision) & reciver == 0  )
     {
         myTime=myTimer;
         guiText.text = myTime.ToString();
         reciver=1;
         if( score > PlayerPrefs.GetInt("High Score")) {     
     PlayerPrefs.SetInt("High Score", score);
     highscore = score;
     }    
     }
 }

Also, does it make any difference if i am trying to implement this with multiple levels?

Thanks in advance.

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 Julien-Lynge · Mar 04, 2013 at 02:11 AM 0
Share

I'm sorry, but you didn't ask a question.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by utAlpha09 · Mar 04, 2013 at 02:51 AM

Guess i didn't. In a nutshell, this code is not saving the values to the player preferences, and i don't know why. Is it something obvious or is there something that i am forgetting.

Comment
Add comment · Show 1 · 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 robertbu · Mar 04, 2013 at 02:59 AM 0
Share

You should add content to your question by using the "add new comment" button rather than as an answer. By adding it as a answer, it is marked as answered and it will receive less traffic. You can use the "more" dropdown to change it to a comment.

As for your question, I cannot pinpoint your specific problem, but the code is a bit strange. Typically the high score would be loaded in a Start(). And the new high score would be set at the end of a level and/or in OnApplicationQuit(). You are getting or setting the score every frame. I don't know the structure of your game, but I don't see anything that is incrementing the score.

Note I also read (but cannot remember where) that the preference are not written out to the permanent store immediately after they are set.

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

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Highscore PlayerPrefs not working on iOS 0 Answers

I'm trying to set a high score but I can't display it in another scene? 2 Answers

Playerprefs not work!? 0 Answers

One highscore player pref setting highscores for all levels 1 Answer

PlayerPrefs.GetInt does not show up 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