- Home /
Question by
SonicDirewolf · Oct 26, 2017 at 06:18 PM ·
textplayerprefsscore
Score Text not Updating
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");
}
}
Comment
Answer by unidad2pete · Oct 26, 2017 at 06:24 PM
I dont read all, but try :
PlayerPrefs.Save();
next line to each modified value, before to exit.
Your answer
Follow this Question
Related Questions
Little Issue with Best Score by Playerprefs 0 Answers
3D ^ Custom GUIText/Label or Making a Score System without using GUI? 0 Answers
Whats better Playerprefs or Text 1 Answer
Code Not working 1 Answer