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 Crispinator · Feb 22, 2014 at 02:03 AM · savescorehighscores

Saving Highscore

I have code that calculates score based on distance. How would I go about getting this code to remember the high score after the game is quit? Thanks in advance.

 #pragma strict
 
 var player : Transform;
 
 
 function Update () 
 {
 var dist : int = Vector3.Distance(player.position, transform.position);
         print ("Distance : " + dist);
 
 var score : int = dist / 5.5;
         
 guiText.text = "Score : " + score;
 
 }
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
6

Answer by POLYGAMe · Feb 22, 2014 at 02:07 AM

Use player prefs:

 PlayerPrefs.SetInt("score", score); // The first is the string name that refers to the saved score, the second is your score variable (int)
 PlayerPrefs.Save()

Then to load it when you start next time (maybe in function Start()):

 score = PlayerPrefs.GetInt("score");

Please note that code is off the top of my head. The syntax/letter capitalization could be a bit off.

Comment
Add comment · Show 8 · 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 immersiveGamer · Feb 22, 2014 at 02:35 AM 0
Share

This is a good first implementation of saving. Though depending on the size of the game and target platform you will want to look into serialization for saving your stats and other objects.

avatar image POLYGAMe · Feb 22, 2014 at 02:38 AM 0
Share

I figure in this case player prefs is perfectly adequate :)

avatar image Crispinator · Feb 22, 2014 at 04:23 AM 0
Share

I must have something wrong. This is what I have.

  #pragma strict
     
     var player : Transform;
     var highscore : int;
     var highscoreGUI : boolean = false;
     var scoreGUI : boolean = false;
     
     
     function Update () 
     {
     if(scoreGUI)
         {
             var dist : int = Vector3.Distance(player.position, transform.position);
                 print ("Distance : " + dist);
     
             var score : int = dist / 5.5;
             
             guiText.text = "Score : " + score;
     
             if(highscore > score)
                 {
                 highscore = score;
                 PlayerPrefs.SetInt("highscore", score);
                 PlayerPrefs.Save();
             
                 }
         }
     
     if(highscore)
         {
             guiText.text = "highscore : " + score;
         }

     }
avatar image POLYGAMe · Feb 22, 2014 at 05:17 AM 0
Share

Well your logic is wrong for a start. It should be if score > high score, then high score = score, or else your high score will always be the same as score or lower. Other than that, it will work. You're not loading the high score though, just saving.

Also, probably best not to use PlayerPrefs.Save() in update like that. Just use it at the end of the game/turn, as it can cause hiccups, due to writing to disk.

avatar image Crispinator · Feb 22, 2014 at 02:16 PM 0
Share

Thanks! I got the code working now.

Show more comments
avatar image
0

Answer by jpsmarinho812 · Feb 22, 2014 at 03:40 AM

POLYGAme is correct. My I think that without PlayerPrefs.Save() works correctly. I used in my game and works.

If you need to test if a variable already exists, use PlayerPrefs.HasKey("score");

Use this before PlayerPrefs.GetInt("score");

Comment
Add comment · Show 2 · 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 SnotE101 · Mar 06, 2014 at 12:16 AM 0
Share

Could you post what your final script was?

avatar image jpsmarinho812 · Mar 06, 2014 at 12:40 AM 0
Share
 public int GetScore () {
         if(!PlayerPrefs.Has$$anonymous$$ey("Score")){
             PlayerPrefs.SetInt("Score",0);
         }
         return PlayerPrefs.GetInt("Score");
     }
     
 public void SetScore(int value){
             PlayerPrefs.SetInt("Score",value);
         }

In this case, if you call GetScore() before of SetScore(), the funcion will create the variable Score with value equals zero.

You can learn more about persistance date in here: http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/persistence-data-saving-loading

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

23 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

high scores scene 2 Answers

PlayerPrefs HighScore problems, it doesn't work. 4 Answers

Highscoring and changing scripts (Java/UnityScript) 1 Answer

whats wrong with my high score code? 1 Answer

How can i get the following script back on track to being a single level high score saver 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