Highscore saving displaying

Hi peeps, i got a problem.

i am making a endless jumper (2d)
i have the score count based on how long you survive,
and i have a death trigger on the bottom of my “Main camera”
when the player collides with it i am loading the so called “death menu” where i have the options play again and main menu, and also a text that says highscore.

basically i want to save my current score when colliding with death trigger and set that to the highscore text in my other scene (death menu), and if i beat it on my next attempt i want it to change, and if i don’t, i want it to stay the same, so basically a standard, saving and displaying Highscore function.

i am using c# and i really don’t have a clue how to write it.

What are you using to count the score? Integer, float?

Based on that, you could use PlayerPrefs.

Let’s say your highscore is currently represented with an integer.

In your trigger function:

If(currentScore > PlayerPrefs.GetInt("HighScore")) {
PlayerPrefs.SetInt("HighScore", currentScore);
PlayerPrefs.Save();
}

“currentScore” should be replaced with whatever variable you’re using to count your score.

In your menu, assuming you’re using the UI text component to display highscore. Put this in start or wherever you feel suitable :

highscoreText.text = PlayerPrefs.GetInt("HighScore").ToString();

here, “highscoreText” should be replaced with whatever variable represents your text component, if you are caching it.

Currently its a float, but i could change it, but do you know how to do it when my score count is a float value

Does something like this help? It would allow you to pass the score value between scenes but it wouldn’t save it between game reboots.

Just replace the PlayerPrefs.GetInt parts with GetFloat and SetInt with SetFloat