Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Highscore saving displaying

Discussion in 'Scripting' started by EmilKihlberg, Nov 8, 2015.

  1. EmilKihlberg

    EmilKihlberg

    Joined:
    Oct 28, 2015
    Posts:
    11
    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.
     
  2. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    990
    What are you using to count the score? Integer, float?

    Based on that, you could use PlayerPrefs.
    http://docs.unity3d.com/ScriptReference/PlayerPrefs.html

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

    In your trigger function:
    Code (CSharp):
    1. If(currentScore > PlayerPrefs.GetInt("HighScore")) {
    2. PlayerPrefs.SetInt("HighScore", currentScore);
    3. PlayerPrefs.Save();
    4. }
    "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 :

    Code (CSharp):
    1. highscoreText.text = PlayerPrefs.GetInt("HighScore").ToString();
    here, "highscoreText" should be replaced with whatever variable represents your text component, if you are caching it.
     
  3. EmilKihlberg

    EmilKihlberg

    Joined:
    Oct 28, 2015
    Posts:
    11

    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
     
  4. Slaghton

    Slaghton

    Joined:
    Sep 9, 2013
    Posts:
    139
  5. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    990
    Just replace the PlayerPrefs.GetInt parts with GetFloat and SetInt with SetFloat