Quantcast
Channel: Questions in topic: "playerprefs"
Viewing all articles
Browse latest Browse all 1333

Score Text not Updating

$
0
0
I have a highscore text that I would like to show on the game over panel. The on screen score works fine, but the HighScore won't update. I've assigned all the game objects to their right places. Here are my two scripts. One updates the score and stores it in Player prefs. The other displays the highscore in a panel. using UnityEngine; using System.Collections; public class ScoreManager : MonoBehaviour { public static ScoreManager instance; public int score; void Awake(){ if (instance == null) { instance = this; } } // Use this for initialization void Start () { score = 0; PlayerPrefs.SetInt ("Score", 0); } // Update is called once per frame void Update () { } public void IncrementScore(){ score++; } public void StopScore(){ PlayerPrefs.SetInt ("Score", score); if (PlayerPrefs.HasKey ("HighScore")) { if (score > PlayerPrefs.GetInt ("HighScore")) { PlayerPrefs.SetInt ("HighScore", score); } } else { PlayerPrefs.SetInt ("HighScore", score); } } } Here's the second one. using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; public class UiManager : MonoBehaviour { public static UiManager instance; public Text scoreText; public Text highScoreText; public GameObject Panel; void Awake(){ if (instance == null) { instance = this; } } // Use this for initialization void Start () { } // Update is called once per frame void Update () { scoreText.text = ScoreManager.instance.score.ToString (); } public void GameOver(){ Panel.SetActive (true); highScoreText.text = PlayerPrefs.GetInt ("HighScore").ToString (); } public void Replay(){ SceneManager.LoadScene ("Game"); } public void Menu(){ SceneManager.LoadScene ("Menu"); } }

Viewing all articles
Browse latest Browse all 1333

Trending Articles