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

saving and displaying high scores for multiple levels

Discussion in 'Scripting' started by jbowers74321, Jan 13, 2020.

  1. jbowers74321

    jbowers74321

    Joined:
    Oct 30, 2019
    Posts:
    25
    can someone show me how to save a high score for an endless runner game for multiple scenes like one for a motomadness level and one for a desert level and them display them in a text UI on a level select screen?
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You can use PlayerPrefs and save the scores as separate ints, or serialize all the high scores into a single string. See any of the many PlayerPrefs tutorials.
     
    Kurt-Dekker likes this.
  3. jbowers74321

    jbowers74321

    Joined:
    Oct 30, 2019
    Posts:
    25
    I can't figure out how to save it for multiple scenes I have got it for one scene and it works but not for multiple the high score either doesn't change or it will save one but clear the other or they are both the same.
     
  4. VSMGames

    VSMGames

    Joined:
    Jan 12, 2020
    Posts:
    47
    With playerprefs you can create multiple separate variables. For example for your motomadness level PlayerPrefs.SetInt("motomadnessHighscore", highscore) and your desert level PlayerPrefs.SetInt("desertHighscore", highscore). In the level select scene load both of them level1Highscore = PlayerPrefs.GetInt(... ) and level2Highscore = ..... Then display the results.
     
  5. jbowers74321

    jbowers74321

    Joined:
    Oct 30, 2019
    Posts:
    25
    I have done that but what I can't figure out is how to get PlayerPrefs motomadness set to that specific scene do i need multiple score scripts for my player on each scene or can I do it on one script? I have a high score working but it changes both PlayerPrefs motomadness and desert. I just can't figure out how to set PlayerPrefs motomadness to the motomadness scene.
     
  6. VSMGames

    VSMGames

    Joined:
    Jan 12, 2020
    Posts:
    47
    You can have one highscore script which depending on the scene loaded with an if statement sets the motomadness or desert playerprefs.
     
    Joe-Censored likes this.
  7. jbowers74321

    jbowers74321

    Joined:
    Oct 30, 2019
    Posts:
    25
    so would this be right?
    if SceneManager.GetActiveScene() == motomadness;
    {
    PlayerPrefs.SetInt("motomadnessHighscore", highscore);
    }
     
  8. VSMGames

    VSMGames

    Joined:
    Jan 12, 2020
    Posts:
    47
    You will probably have to include ".name" after the brackets and put the name into quotation marks. More reasonable would be to get the active scene name only once in the Start function and then use that in the if statement.
     
    Joe-Censored likes this.
  9. Inxentas

    Inxentas

    Joined:
    Jan 15, 2020
    Posts:
    275
    I would go with JSON serialization. That way you can structure your save data anyway you want, in your case by Scene. The process is a little more complex then PlayerPrefs and I am learning it myself at the moment (so my code is still a mess) but this documentation is a good starting point.

    The gist of it is that you can serialize simple objects like below example into JSON...

    Code (CSharp):
    1. [System.Serializable]
    2. public class Levels
    3. {
    4.     public List<Level> data;
    5. }
    6.  
    7. [System.Serializable]
    8. public class Level
    9. {
    10.     public int id;
    11.     public int score;
    12. }
    ...which you can then store as a binary format. I understand that your primary problem is binding the data to the actual Scene, but if you work through this long but worthwhile page you will understand the process of storing more complex data structures, which is nice to understand in the long term. PlayerPrefs is more or less intended for single variables that aren't mission critical. I would put things like graphical preferences and sound volumes in PlayerPrefs and store any user data using the JSON method.
     
    charmseer likes this.
  10. kiritsugucarl

    kiritsugucarl

    Joined:
    Sep 4, 2021
    Posts:
    1
    i know im terribly late, but man, thanks for the link, I did it better now. <3