I using scoreManager script and it works but i couldn’t add high score section. In fact I added but it doesn’t work. how can i do this high score section. my score script is below.
ScoreManager Script:
public static int score; // The player's score.
Text text; // Reference to the Text component.
void Awake ()
{
// Set up the reference.
text = GetComponent <Text> ();
// Reset the score.
score = 0;
}
void Update ()
{
// Set the displayed text to be the word "Score" followed by the score value.
text.text = "Score: " + score;
}
Create a new Text object in your UI.
Make modifications to your code to add a highscore variable, loading it in awake and updating it if score > highscore.
Finally, set up the reference to your Text object in the inspector.
using UnityEngine;
using UnityEngine.UI;
public class Score : MonoBehaviour
{
static int highscore;
public static int score;
Text text;
// Set highscoreText up in the inspector.
public Text highscoreText;
void Awake()
{
text = GetComponent<Text>();
score = 0;
highscore = PlayerPrefs.GetInt("highscore");
}
void Update()
{
if (score > highscore)
{
highscore = score;
PlayerPrefs.SetInt("highscore", highscore);
highscoreText.text = "Highscore: " + highscore;
}
text.text = "Score: " + score;
}
}