I am making a dodging game and I was working on the scoring system until I needed to call Public TextMeshProUGUI HighScore;
to another script, I have been trying to find a way to add it and make it display what it does in my Timer script but it doesn't seem to be working does anyone know how to fix this?I am pretty new to C# sorry.
Asked
Active
Viewed 113 times
0

IU64
- 11
- 2
-
What have you tried? Where exactly are you getting stuck? :) – Jay Feb 25 '22 at 13:26
-
in my game, there is a score timer so when you collide with the enemy object it sends you to a different scene called the main menu and I am trying to get but the timer script I already made a high score so what I need is another script that can display the HighScore or call the variable as text or TextMeshProUGUIbut all guides just show how to add values and not how to call the variable and be able to put a text and it will show highscore from the timer script – IU64 Feb 25 '22 at 13:42
1 Answers
0
If you're wanting to send values to another scene, this can be a little difficult.
You can tell an object storing the value to persist between scene loads using Object.DontDestroyOnLoad.
However, if this is for a highscores value, it might be worth saving this value externally. Ideally you would set up a proper save system, but a neat hack for storing a single value that I think could be appropriate in this case would be using PlayerPrefs.
Like so:
private void SaveHighscore(int score)
{
PlayerPrefs.SetInt("HighScore", score);
}
Then in your other scene when you want to display the value something like
private void ShowHighscore()
{
var score = PlayerPrefs.GetInt("HighScore");
_highScoreGUI.text = $"Highscore is: {score}"; // _highScoreGUI is any text ui element
}

Jay
- 2,553
- 3
- 17
- 37
-
Some people would (rightly) criticise this as a misuse of `PlayerPrefs`, but if you are pretty new to Unity and making projects to learn, it's totally fine. Just know there are better but more complex ways to store data out there. – Jay Feb 25 '22 at 13:56