- Home /
Local highscore issues
Hello, I'm here, once again, to ask for help.
I'm working on a local highscore for a game that I'm working on, but after spending the last few hours searching for a good solution, without any luck, i decided to ask.
The curent code that i got for the highscore script is the following:
//Linking the text that needs to show the highscore
public Text highscore;
//If the score is better than the old highscore it needs to be replaced
if (score > PlayerPrefs.GetInt ("highscore")) {
PlayerPrefs.SetInt ("highscore", score);
}
// Get the highscore
highscore = PlayerPrefs.GetInt ("highscore");
//Set the text of the text.
highscore.text = "Highscore: " + highscore;
My issue is:
Cannot implicitly convert type 'int' to 'UnityEngine.UI.Text'
And it leads to this line with the error: highscore = PlayerPrefs.GetInt ("highscore");
Is there any way to solve this issue, or maybe another way to make a local highscore?
Thanks.
Regards. Tech.
My full code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class EndGameScore : MonoBehaviour {
public Text Finalscore;
public Text highscore;
int score = 0;
// Use this for initialization
void Start () {
if (score > PlayerPrefs.GetInt ("highscore")) {
PlayerPrefs.SetInt ("highscore", score);
}
highscore = PlayerPrefs.GetInt ("highscore");
score = PlayerPrefs.GetInt ("Score");
Finalscore.text = "Your score: " + score;
highscore.text = "Highscore: " + highscore;
}
Answer by Landern · Nov 16, 2014 at 02:10 AM
highscore is of type Text and GetInt from PlayerPrefs returns an int. You need to transform the type.
//Linking the text that needs to show the highscore
public Text highscore;
//If the score is better than the old highscore it needs to be replaced
if (score > PlayerPrefs.GetInt ("highscore")) {
PlayerPrefs.SetInt ("highscore", score);
}
// Get the highscore
// Set the text of the text.
highscore.text = "Highscore: " + PlayerPrefs.GetInt("highscore").ToString();
EDIT:
Using your code, i'd probably modify it to do the following:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class EndGameScore : MonoBehaviour {
public Text Finalscore;
public Text highscore;
int score = 0;
int oldHighScore = 0;
// Use this for initialization
void Start () {
oldHighScore = PlayerPrefs.GetInt ("highscore"); // You're already going to call this multiple times it seems, might as well stuff it in a variable.
score = PlayerPrefs.GetInt ("Score");
if (score > oldHighScore) {
PlayerPrefs.SetInt ("highscore", score);
PlayerPrefs.Save();
}
Finalscore.text = "Your score: " + score;
highscore.text = "Highscore: " + (score > oldHighScore ? score : oldHighScore);
}
}
The part that you see setting the highscore.text is a ternary operator, it's short hand for a if/else statement, in the case above, if score is greater than oldHighScore, then use the value from score if not, use oldHighScore since it is still the king.
Hello, Thanks alot for the answer.
You did help me solve the issue, and for that, thanks alot.
But now I do have another question, seeing as it's currently staying at 0, i tried taking a few games to see if that made it update, however, there was no success.
Any suggestions / ideas on how this might be fixed?
are you calling PlayerPrefs.Save(); when you want to save them?
I wasen't at my brighetest moment before (Sorry, It's 4am)
After fooling around with it, i got it added, tried again a few times, however, it's still 0.
Oh, god!
Your updated post solved my issues right away, and that script works like a charm.
That's a pretty good way to get it updated on, guess you learn something new every day, as i tought that the other script did the same thing, but well, you learn something new every day.
Thanks a lot man!
Your answer
![](https://koobas.hobune.stream/wayback/20220613171625im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
High Score Name entry 1 Answer
How can i get the following script back on track to being a single level high score saver 0 Answers
Accessing local system ( File Browser ) 2 Answers
Load text from a file and disply it 0 Answers
Guitext for score over time. 2 Answers