A way to move "Highscore" Text to Game Over Screen?
Hello Really Stupid and a beginner here, So i have a script for recording High scores. But i kinda want to do something different for it.
I have the functions for it in a single script. But i want it to split up into two different UI. First one was of course the in-game one (which will display the current score) That one i already got fixed up.
And the one for the game-over screen (High score in the Game Over UI) My problem starts here because the Game over screen i have is deactivated at the Start. So that when the Player Dies, The Screen is just going to get enabled, which will execute the Animations and the Sound for it. The thing is i want it to be with that game over screen. If that makes sense. But i can't put it in there because it is disabled at first. Which will make the script for it disabled thus not getting the current Score. I was wondering if is there a way to make this work without remaking the game over screen and changing it to a new different script. Like maybe running the high score screen in the background hidden but mirroring the score to the text that appears when the Game over screen pops out. I don't know lmao. But maybe something similar or another solution that can work is possible?
SCRIPT DOWN HERE:
public class ScoreManager : MonoBehaviour {
// pardon the the stupid code
public static ScoreManager instance;
public TMP_Text scoreText;
public TMP_Text highscoreText;
int score = 0;
int highscore = 0;
private void Awake()
{
instance = this;
}
// Start is called before the first frame update
void Start()
{
highscore = PlayerPrefs.GetInt("highscore", 0);
scoreText.text = score.ToString();
highscoreText.text = "HIGHSCORE:" + highscore.ToString();
}
// Update is called once per frame
public void AddPoint()
{
score += 50;
scoreText.text = score.ToString();
if (highscore < score)
PlayerPrefs.SetInt("highscore", score);
}
public void HighScoreReset()
{
PlayerPrefs.DeleteKey("highscore");
highscoreText.text = "0";
// Down here was just for future use idk
//PlayerPrefs.DeleteAll(); //delete everything from username, etc
}
}
Answer by lvskiprof · Aug 27, 2021 at 04:10 AM
I do this by having the score kept in a Singleton that exists on the Game and Game Over screens. You also need that if you have multiple levels.
Thank you so muchh! I'll try and learn about Singletons now! Hopefully i figure it out.
Your answer
Follow this Question
Related Questions
Highscore - BestTime 1 Answer
Highscore Not Updating Upon Death 1 Answer
how do i store highscore locally C# (SIMPLE) 3 Answers
online highscore unity 5.1 1 Answer
UI Text not displaying public int but will display private int 1 Answer