- Home /
Can anyone tell me what is wrong with my high score displaying script?
My HIGH SCORE stuck at "0".
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ScoreController : MonoBehaviour {
public Text ScoreText;
public int score;
private int Count = 2;
public Text TotalScoreText;
public Text HighScoreText;
public int HighScore;
void Start () {
score = 0;
UpdateScore ();
if(score>PlayerPrefs.GetInt ("HIGHSCORE_KEY",0))
{
PlayerPrefs.SetInt ("HIGHSCORE_KEY",score);
}
HighScore = PlayerPrefs.GetInt ("HIGHSCORE_KEY",0);
}
public void UpdateScore() {
ScoreText.text = "Score: " + score;
TotalScoreText.text = "TOTAL SCORE -: " + score;
HighScoreText.text = "HIGH SCORE -: " + HighScore;
}
public void AddScore( )
{
score += Count;
UpdateScore ();
}
}
Answer by sas_88 · Jul 13, 2015 at 04:40 AM
Hi, In start function u have assigned the assigning the score=0 and then called UpdateScore ().Then the value of score remains same as zero it does not get updated.
So the PlayerPrefs of "HIGHSCORE_KEY" value remains zero.
U have missed to call the function for adding score.
After updating score,"HIGHSCORE_KEY" PlayerPrefs value has to assign to HighScore.
check with the code its updating.
void Start ()
{
score = 0;
UpdateScore ();
AddScore ();
}
public void UpdateScore()
{
ScoreText.text = "Score: " + score;
TotalScoreText.text = "TOTAL SCORE -: " + score;
HighScoreText.text = "HIGH SCORE -: " + HighScore;
}
public void AddScore( )
{
score += Count;
if(score>PlayerPrefs.GetInt ("HIGHSCORE_KEY",0))
{
PlayerPrefs.SetInt ("HIGHSCORE_KEY",score);
}
HighScore = PlayerPrefs.GetInt ("HIGHSCORE_KEY",0);
UpdateScore ();
}
Answer by Exceptione · Jul 13, 2015 at 10:59 AM
You're only checking if score is higher than score in start, try moving it into the AddScore(). This isn't the most efficient way of handling a high score, you would want to check it at the end of the game but I assume your code is just temporary testing.
public void UpdateScore() {
if(score>PlayerPrefs.GetInt ("HIGHSCORE_KEY",0))
{
PlayerPrefs.SetInt ("HIGHSCORE_KEY",score);
}
HighScore = PlayerPrefs.GetInt ("HIGHSCORE_KEY",0);
ScoreText.text = "Score: " + score;
TotalScoreText.text = "TOTAL SCORE -: " + score;
HighScoreText.text = "HIGH SCORE -: " + HighScore;
}
Answer by bigbadtrumpet125 · Jul 13, 2015 at 11:00 AM
You are never setting HIGHSCORE_KEY after you add you call AddScore, I would add this to the end of your UpdateScore function.
HighScore = PlayerPrefs.GetInt ("HIGHSCORE_KEY",0);
if(score > HighScore)
{
PlayerPrefs.SetInt("HIGHSCORE_KEY", score);
}
Your answer
Follow this Question
Related Questions
Saving Score In Player Prefs? 3 Answers
How many different scripts do I need to write to create a highscore system? 1 Answer
Distance and Best Distance 1 Answer
high scores scene 2 Answers