- Home /
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);
}
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.
check this http://answers.unity3d.com/questions/252209/high-score-list.html... you can store n number of high scores!
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);
}
I see you took my advice in setting the past high scores and comparing the new one :)
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 :)
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).
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 , :(
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.
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)
}
}
}
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);
}
If you have your answer please accept the answer that help so that the topic is closed :)
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.
Your answer
Follow this Question
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