- Home /
high scores scene
I'm working on a game in which the score is the amount of time the player is alive. So far I am using PlayerPrefs to save the score with this script: EDIT:
private var alive = true; function OnCollisionEnter(collision : Collision) {
if(collision.gameObject.tag == "blocks") { Time.timeScale = 0.000001; audio.Pause (); Invoke("thescore",0); alive = false; } } function thescore () { var score : float = Time.timeSinceLevelLoad;
if( score > PlayerPrefs.GetFloat("Score1") ){ PlayerPrefs.SetFloat( "Score5", PlayerPrefs.GetFloat("Score4") ); PlayerPrefs.SetFloat( "Score4", PlayerPrefs.GetFloat("Score3") ); PlayerPrefs.SetFloat( "Score3", PlayerPrefs.GetFloat("Score2") ); PlayerPrefs.SetFloat( "Score2", PlayerPrefs.GetFloat("Score1") ); PlayerPrefs.SetFloat("Score1", score); } else if( score > PlayerPrefs.GetFloat("Score2") ){ PlayerPrefs.SetFloat( "Score5", PlayerPrefs.GetFloat("Score4") ); PlayerPrefs.SetFloat( "Score4", PlayerPrefs.GetFloat("Score3") ); PlayerPrefs.SetFloat( "Score3", PlayerPrefs.GetFloat("Score2") ); PlayerPrefs.SetFloat("Score2", score); } else if( score > PlayerPrefs.GetFloat("Score3") ){ PlayerPrefs.SetFloat( "Score5", PlayerPrefs.GetFloat("Score4") ); PlayerPrefs.SetFloat( "Score4", PlayerPrefs.GetFloat("Score3") ); PlayerPrefs.SetFloat("Score3", score); } else if( score > PlayerPrefs.GetFloat("Score4") ){ PlayerPrefs.SetFloat( "Score5", PlayerPrefs.GetFloat("Score4") ); PlayerPrefs.SetFloat("Score4", score); } else if( score > PlayerPrefs.GetFloat("Score5") ){ PlayerPrefs.SetFloat("Score5", score); } }
Here's an example of the new High score script that is attached to one of my guitexts in the high score scene: function Start () { guiText.text = (PlayerPrefs.GetFloat("score1")).ToString (); }
My new problem is that the high scores won't update, but instead stay at zero all the time.
It looks like all the PlayerPrefs you are setting in your previous scene have a capital "S" in score, but your sample code from your high score scene is trying to pull PlayerPrefs with a lowercase "s", is that it?
Success! Thanks a lot for your help. Would never have figured it out on my own.
No problem! Glad to help; I thought it was an interesting challenge that I might have use sometime.
Answer by PrimeDerektive · Jan 03, 2011 at 09:49 PM
Couple things:
Your going to need 5 PlayerPrefs, for the top 5 scores. I'll call them score1, score2, score3, and so on.
Second, you'll want to store them in PlayerPrefs as floats, instead of strings, and just do the .ToString() for you GUI after you retrieve them for the high score scene (that way, you can actually do math comparisons with them).
Then you'll probably want to do something like this (EDIT: after Ej's comment I realized you have to shift all the scores down one, from the bottom up, if you are overwriting a score [unless its score 5, because theres no score 6 to shift it to, so it just gets dumped] ):
var score : float = Time.timeSinceLevelLoad;
if( score > PlayerPrefs.GetFloat("Score1") ){ PlayerPrefs.SetFloat( "Score5", PlayerPrefs.GetFloat("Score4") ); PlayerPrefs.SetFloat( "Score4", PlayerPrefs.GetFloat("Score3") ); PlayerPrefs.SetFloat( "Score3", PlayerPrefs.GetFloat("Score2") ); PlayerPrefs.SetFloat( "Score2", PlayerPrefs.GetFloat("Score1") ); PlayerPrefs.SetFloat("Score1", score); } else if( score > PlayerPrefs.GetFloat("Score2") ){ PlayerPrefs.SetFloat( "Score5", PlayerPrefs.GetFloat("Score4") ); PlayerPrefs.SetFloat( "Score4", PlayerPrefs.GetFloat("Score3") ); PlayerPrefs.SetFloat( "Score3", PlayerPrefs.GetFloat("Score2") ); PlayerPrefs.SetFloat("Score2", score); } else if( score > PlayerPrefs.GetFloat("Score3") ){ PlayerPrefs.SetFloat( "Score5", PlayerPrefs.GetFloat("Score4") ); PlayerPrefs.SetFloat( "Score4", PlayerPrefs.GetFloat("Score3") ); PlayerPrefs.SetFloat("Score3", score); } else if( score > PlayerPrefs.GetFloat("Score4") ){ PlayerPrefs.SetFloat( "Score5", PlayerPrefs.GetFloat("Score4") ); PlayerPrefs.SetFloat("Score4", score); } else if( score > PlayerPrefs.GetFloat("Score5") ){ PlayerPrefs.SetFloat("Score5", score); }
Wouldn't that just overwrite one of the scores? $$anonymous$$g. I get a score greater than score2, so I overwrite that score, but the previous score2 was greater than score3, score4, and score5, but now we have lost that information?
Holy crap! $$anonymous$$assive oversight on my part... Thanks Ej. Let me think about this for a second...
Ok I've read the script you've provided and it makes sense to me. I'll try it out and let you know what happens. Thanks for the info.
$$anonymous$$. I tried what you said and it's not quite working. There are no compiler errors. $$anonymous$$y highscore scene has 5 gui texts in it each with a different script attached to it, an example being: function Start () { guiText.text = (PlayerPrefs.GetFloat("score1")).ToString (); } However, the scores are always the default of zero and never change. Each score is simply zero and I don't know where to go from here.
Answer by Jackolanten · Apr 04, 2013 at 12:28 AM
hi i already do that script above but when my player finish all stage all score from 1 to 5 is same as high score, but when i game over the socre is normal.. i dunno whats wrong with the script