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

Looking to store top 5 highscores into the UI main menu

Discussion in 'Scripting' started by Kibbles2662, Nov 30, 2015.

  1. Kibbles2662

    Kibbles2662

    Joined:
    Nov 30, 2015
    Posts:
    4
    Basically as the title says, I'm trying to store and show the player their top 5 highscores under the main menu, in the scoreboards section.
    As changeable text that will update when any of the highscores are beaten!

    Any help would be much appreciated

    Kibbles

    PS: I have coded properly in C# in a few years
     
  2. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,732
    There are numerous ways to approach this.

    What have you already tried and what problems have you encountered?
     
  3. Kibbles2662

    Kibbles2662

    Joined:
    Nov 30, 2015
    Posts:
    4
    I haven't really tried anything because i have no idea on how to approach it! I know the high scores will stored in variables which will be called from the main code of the game! but apart from that I am lost! Sorry
     
  4. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,732
    I'm not going to write the code from scratch for you. In your opening post you indicate that you have some years of C# programming, in which case you ought to have a working knowledge of arrays, lists and classes.

    Therefore I would suggest you approach it along these lines :-
    Define a PlayerScore class that exposes public variables for the name and score (at least).
    Create a list of PlayerScore (sorted by PlayerScore.score value) and add new instances of PlayerScore to it as needed.
    You can iterate this list to build your high score table/display.
    To persist the scores between game sessions you will need to serialize the list to some kind of external storage, playerprefs is probably the easiest to start with.

    If you get stuck on any of the above tasks, post your code, and myself or someone else should be able to help further.
     
  5. Kibbles2662

    Kibbles2662

    Joined:
    Nov 30, 2015
    Posts:
    4
    thank you for your help!!! I don't expect anyone to write the code for me haha your guidelines are very helpful and I will post an update of the code I make for some feedback
     
    Munchy2007 likes this.
  6. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,732
    You're welcome, good luck with your project :)
     
  7. Kibbles2662

    Kibbles2662

    Joined:
    Nov 30, 2015
    Posts:
    4
    HEY! So I managed to get my scores working, through it increasing in time and through pickups. I'm a little confused on how to get the score from the game end, saving it and then checking it in another UI script with the highscores than displaying the 5 top ones.
    My script for getting the scores working:

    using UnityEngine;
    using UnityEngine.UI;
    using System.Collections;

    public class ScoreManager : MonoBehaviour
    {
    public Canvas GUI_HUD;
    public Text ScoreText;

    public float ScoreCount;
    public float HiScoreCount;

    public float PointsPerSec;

    public bool ScoreIncrease;

    // Use this for initialization
    void Start ()
    {
    GUI_HUD = GUI_HUD.GetComponent<Canvas> ();
    ScoreText = ScoreText.GetComponent<Text> ();
    ScoreText.enabled = true;
    PointsPerSec = 10;
    ScoreIncrease = true;
    }

    // Update is called once per frame
    void Update ()
    {
    if (ScoreIncrease)
    {
    ScoreCount += PointsPerSec * Time.deltaTime;
    }
    ScoreText.text = "Score: " + Mathf.Round (ScoreCount);

    if (ScoreCount > HiScoreCount)
    {
    HiScoreCount = ScoreCount;
    PlayerPrefs.SetFloat("HighScore", HiScoreCount);
    }
    }

    public void AddScore(int PointsToAdd)
    {
    ScoreCount += PointsToAdd;
    }
    }
     
  8. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,732
    First of all, please take some time to learn how to use code tags ;) http://forum.unity3d.com/threads/using-code-tags-properly.143875/

    But, to answer your question, at the moment when you save the score you are overwriting the previous score, therefore you can only ever retrieve the last score saved.

    What you need to do is adapt your high score function to save the score to a list or array of scores. You can then sort this by score value and use it to populate your high score table.

    When you want to save/load the scores between scenes and game sessions, use a for/next loop to iterate through the last and save or load each element.