- Home /
Highscoring and changing scripts (Java/UnityScript)
I have a score system that records score in the game, but i would like to use playerprefs to check the current score and if higher that previous highscore, set the guitext to a new highscore
scoring.js
#pragma strict
var Counter : int = 0;
function addscore () {
Counter+= 1;
guiText.text = "score:"+Counter;
}
function Update () {
Debug.Log("updated Guitext.text");
guiText.text = "Score:"+Counter;
}
function endlevel (){
guiText.text = "0";
Counter = 0;
}
highscore.js
#pragma strict
var High : int;
function Start () {
guiText.text = ""+High;
}
thanks for any help
Answer by Landern · Nov 26, 2014 at 12:51 AM
Well, update is called once a frame, if you're using Update and your addscore function, i would ditch the guiText.text change in addscore, such a waste of time as it will be picked up on the next frame Update function call.
#pragma strict
var Counter : int = 0;
function addscore () {
Counter += 1;
}
function Update () {
Debug.Log("updated Guitext.text");
guiText.text = "Score:" + Counter.ToString();
}
function endlevel (){
var oldHighscore : int = PlayerPrefs.GetInt("Highscore", 0); // second parameter is for default value if not found in player prefs.
if (Counter > oldHighscore) {
PlayerPrefs.SetInt("Highscore", Counter); // if the current score(counter) is greater than the old highscore, then you have beaten it.
PlayerPrefs.Save(); // After setting, save/commit the new value(s).
}
guiText.text = "Score:0";
Counter = 0;
}
if you want to pull it in the other script:
#pragma strict
var High : int;
function Start () {
High = PlayerPrefs.GetInt("Highscore", 0); // again, second parameter is default if not found yet.
guiText.text = "Highscore:" + High.ToString();
}
for some reason the highscore script isn't loading the new score
could it be the fact that it changes scenes and then comes back?
Your answer
Follow this Question
Related Questions
Is there a decent tutorial for a local high score table (android)? 1 Answer
PlayerPrefs not saving my variable value for HighScore System 3 Answers
How would I transfer these into PlayerPrefs? 0 Answers
PlayerPref set int and get int 1 Answer
Is it necessary to create an Array() to show high scores in PlayerPref? 1 Answer