- Home /
score board
I trying to make a scoardboard and add a new high score using playerprefs
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement;
public class scoreManager : MonoBehaviour { public static scoreManager instance; public int Score; public int highScore; public Text scoreText; public Text highscoreText; public Text GameOverText;
private void Awake()
{
instance = this;
if (PlayerPrefs.HasKey("highScore"))
{
highScore=PlayerPrefs.GetInt("highScore");
highscoreText.text = highScore.ToString();
}
}
public void AddScore()
{
Score++;
UpdateHighScore();
scoreText.text = Score.ToString();
GameOverText.text = Score.ToString();
}
public void UpdateHighScore()
{
if(Score>highScore)
{
highScore = Score;
highscoreText.text = highScore.ToString();
PlayerPrefs.SetInt("highScore",highScore);
}
}
}
You need to run this after PlayerPrefs.SetInt() to make it stick.
PlayerPrefs.Save();
Your answer
![](https://koobas.hobune.stream/wayback/20220613030849im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to increase score by one per second when you are holding an object 2 Answers
Displaying Score 1 Answer
Changing the UI text value from server to Client side 0 Answers
adjustable point limit 1 Answer
How to continue scoring after game/app has been closed,Making score count after app is closed 0 Answers