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 Omer.Hussain · Mar 18, 2014 at 01:06 PM · playerprefsdatabasehighscoresscoreboard

how to save and retreive high score....

Hi! i have gone through different examples but i couldn't understand that much.. i really need help, In my game i want to save and compare current score and high score. if our current score is greater than high score then show our new high score and start comparing with new current score... here is my script in which i have current score and i have tried to applied playerprefs but couldn't work well .. kindly alter this code i will much glade.. at the end of the code i am displaying the current score .. if it is possible to show high score here kindly help.... #pragma strict

 private    var startTime:int;
 //    var labelPosition : Rect;
     var labelText : String;
     var labelStyle : GUIStyle;
     var score : int;
     var highScore: int=0;
     var realTimeStart: int;
 static    var timer:boolean ;
 
     function Start(){
         startTime= Time.time;
         timer=true;
     }
     function Update()
     {
         if (timer)
         {
             realTimeStart=(Time.time-startTime)*10;
             PlayerPrefs.SetInt("realTimeStart",realTimeStart);
             var GetScore:int= PlayerPrefs.GetInt("realTimeStart");
 
         ////////for high score    
             if(highScore>realTimeStart)
                 {
                     highScore=highScore;
                     PlayerPrefs.SetInt("HighScore",highScore);
                     var GetHS: int = PlayerPrefs.GetInt("HighScore");
                     print("High score" +GetHS);
                     //PlayerPrefs.GetInt("actualscore",highScore);
                 }
             else 
                 {
                     highScore=realTimeStart;
                     PlayerPrefs.SetInt("NewHighScore",highScore);
                     var GetNHS: int = PlayerPrefs.GetInt("NewHighScore");
 //                    print("New High score" +GetNHS);
                 }
 //////        PlayerPrefs.SetInt("HighScore",realTimeStart);
         labelText = realTimeStart.ToString();
         PlayerPrefs.SetString("TimeScore",labelText);
         }
     }      
     function OnGUI()
     {
 
         
         GUI.Label(Rect (Screen.width-125, 30, 130, 25), labelText, labelStyle);
     }
Comment
Add comment · Show 2
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 Dblfstr · Mar 18, 2014 at 01:43 PM 1
Share

Working up a solution should not be too difficult. But you Only need to check the score vs highScore one time. And that is when the game is over. So you check the final score to the highscore. Then you can set your playerprefs. You do not want to get and set playerprefs every update. So maybe when timer = false or something that says the round is over, and the score is the final score.

avatar image flamy · Mar 19, 2014 at 11:07 AM 0
Share

check this http://answers.unity3d.com/questions/252209/high-score-list.html... you can store n number of high scores!

4 Replies

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

Answer by Omer.Hussain · Mar 19, 2014 at 11:05 AM

the only real-world answer these days is to use Parse.com or GameCenter, or google's version of GameCenter.


I some how managed my problim ... on collision or if your player dies or game ends.in that code add this

 function Start(){
         highScore=PlayerPrefs.GetInt("highScore");
                 }

///// then in OnCollisionEnter Function, add this

    currentScore=Timer.realTimeScore;
     if(currentScore>highScore){
           highScore=currentScore;
           PlayerPrefs.SetInt("highScore",highScore);
                 //print(highScore);                                
                     }
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 Calum-McManus · Mar 19, 2014 at 11:06 AM 0
Share

I see you took my advice in setting the past high scores and comparing the new one :)

avatar image Omer.Hussain · Mar 19, 2014 at 11:15 AM 0
Share

No actually this post link text and @Dblfstr helped ... :p

avatar image Calum-McManus · Mar 19, 2014 at 11:18 AM 0
Share

Ah, I assumed other wise as that was posted after you answer, in that case please convert is comment to an answer and accept it, or use the edit drop down to close the question :)

avatar image
0

Answer by Calum-McManus · Mar 18, 2014 at 02:31 PM

You should look in to ArrayPrefs2 (Other wise known as PlayerPrefX), It allows you to save whole arrays with one line of code then call it back in one line, It has saved me allot of hassle!

Link: http://wiki.unity3d.com/index.php/ArrayPrefs2

Or for a single high-score:

 PlayerPrefs.SetInt("HighScore", m_HighScore);
         
 m_HighScore = PlayerPrefs.GetInt("HighScore");

Use an IF Statement to check if the high score is Higher than the last (or lower if its a best time or something).

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 Omer.Hussain · Mar 19, 2014 at 06:09 AM 0
Share

i know about playerpref.setint and GetInt .. but i am actually not implementing them in a right way.... array isn't the solution here :/ ... .. i am storing high score , but every time when i restart the level it start calculating new high score , doesn't compare well .. i tried in different ways , by comparing GetInt(HighScore)with current score ..this is the part i need help

  if(highScore>realTimeStart)
 {
 highScore=highScore;
 PlayerPrefs.SetInt("HighScore",highScore);
 var GetHS: int = PlayerPrefs.GetInt("HighScore");
 print("High score" +GetHS);
 //PlayerPrefs.GetInt("actualscore",highScore);
 }
 else
 {
 highScore=realTimeStart;
 PlayerPrefs.SetInt("NewHighScore",highScore);
 var GetNHS: int = PlayerPrefs.GetInt("NewHighScore");
 // print("New High score" +GetNHS);
 }

kindly look at my code and write according to that , :(

avatar image Calum-McManus · Mar 19, 2014 at 09:57 AM 0
Share

Ok yeah, you are setting a new high score every time, you are checking if your var "highscore" is larger than "realStartTime" and if it is setting it as the high score, you need to use "getint" to get the only highscore first, then compare the score for this game against that score.

avatar image Calum-McManus · Mar 19, 2014 at 10:07 AM 0
Share

The best this to do is that another var that gets set in your start function called maybe "LastHighScore" that is set to PlayerPrefs.GetInt("HighScore"), this way you can compare your new high-score to this one.

 var lastHighScore : int;
 
 function Start()
 {
   lastHighScore = PlayerPrefs.GetInt("HighScore")
 }
 
 function CheckHighScore()
 {
   if(highScore>realTimeStart)
     {
        if(highScore > pastHighScore)
          {
             PlayerPrefs.SetInt("HighScore", highScore)
          }
     }
 }
avatar image
0

Answer by KhShani · Mar 19, 2014 at 10:04 AM

Yes in order to store your values persistently You need to use PlayerPrefs

You can create playerprefs like

if (!PlayerPrefs.HasKey ("HighScore")) { PlayerPrefs.SetInt("HighScore",0);
}

Now in somewhere in the game you may want to called and reset your saved highscore value by doing following if( (PlayerPrefs.GetInt ("HighScore")) > score) { PlayerPrefs.SetInt("HighScore",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 Omer.Hussain · Mar 19, 2014 at 11:01 AM 0
Share

Thankx Every One for your response :)

avatar image Calum-McManus · Mar 19, 2014 at 11:02 AM 0
Share

If you have your answer please accept the answer that help so that the topic is closed :)

avatar image
0

Answer by sshukla480 · Apr 19, 2014 at 06:27 PM

Well, App42 Gaming APIs come truly handy, when we talk about saving the score and displaying the leader-board in much lesser time.To display top scores in your game you can simply use get_top_n_rankers. In Which you can also sort your game scorer by adding custom header for documentation get_top_n_rankers_by_sorting.

Comment
Add comment · 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

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

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

Related Questions

Saving final score and displaying on main menu 1 Answer

A node in a childnode? 1 Answer

HighScore Manage Using PlayerPrefs 1 Answer

Taking high score from anther script and then displaying it 2 Answers

How do I make a highscores board? 3 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