Other
Showing a high score in the menu
Hey guys, I am new in unity and I just can´t find a solution for my problem. I want to display the high score in my menu Scene (I allready got a text that says Highscore:??Highscore??) by using my uiManager script. Can you help me?
using UnityEngine; using System.Collections; using UnityEngine.UI; using UnityEngine.SceneManagement;
public class uiManager : MonoBehaviour {
public Button[] buttons;
public Text scoreText;
public AudioManager am;
bool gameOver;
int score;
void Start () {
gameOver = false;
score = 0;
InvokeRepeating ("scoreUpdate", 1.0f, 0.5f);
}
void Update () {
scoreText.text = "Score " + score;
}
public void Play(){
SceneManager.LoadScene("Game");
}
void scoreUpdate(){
if (gameOver == false) {
score += 1;
}
}
Answer by EDevJogos · Aug 16, 2016 at 11:46 PM
If you want just the Highest score value, you can create a property that will accept only the highest value seted to it:
private int _highScore;
public int highScore
{
get { return _highScore; }
set{ _highScore = value > _highScore ? value : _highScore; }
}
At the end of the match just set:
highScore = score;
//if the current score is greater than the previous one the variable highScore
//will be updated if not will just discard it.
Follow this Question
Related Questions
Script to get Eye Material to move only seems to work in Inspector/Edit Mode 0 Answers
UI Text Not Updating 1 Answer
Better way to edit RawImage texture data?,Better way to edit a RawImage texture? 0 Answers
Image UI not enabling C# SOLVED 1 Answer
TMP text that was changed from script is overridden by the one in editor 0 Answers