Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by dgalbraith3 · Apr 25, 2017 at 06:07 PM · uitimersave datasaveloadhighscores

c# How would I go about doing a High Score Board?

I have been searching for tutorials but I have only been able to find high scores but no high score boards. I was wanting to show the top three high scores in main menu. I am still new to coding and this is the first time I have had to work with something that needed to be saved. Is anyone able to give examples or have any suggestions/tutorials? Any help would be appreciated, thanks in advance!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Vicarian · Apr 25, 2017 at 06:38 PM

@dgalbraith3 In general, if you want to store a collection of objects, and add to them dynamically, I'd recommend using a List. The code below isn't complete by any means, but should introduce a couple ways of getting to store scores and retrieve them. Just add this script to a GameObject in your Hierarchy and run the game. You'll get output in the console showing the result of the code placed in the Start() method.

 using System.Collections.Generic;
 using System.Linq;
 using UnityEngine;
 
 public class HighScores : MonoBehaviour
 {
     // You might change your mind as to the number of high scores to show, so make it
     // easily configurable
     private const int TOP_SCORE_COUNT = 3;
 
     // Store our scores
     private List<PlayerScore> m_scores;

     // Say you have a Text object in the Hierarchy and drag that text object into this field of the script
     [SerializeField] Text m_topScoreLabel;
 
     void Start() {
         m_scores = new List<PlayerScore>();
         
         // Populate m_scores with some test data
         m_scores.Add(new PlayerScore() { playerName = "Billy", score = 40 });
         m_scores.Add(new PlayerScore() { playerName = "Bob", score = 10 });
         m_scores.Add(new PlayerScore() { playerName = "Joe", score = 20 });
         m_scores.Add(new PlayerScore() { playerName = "Ray", score = 50 });
         
         // Retrieve our scores
         PlayerScore[] top3Scores = GetHighScores(TOP_SCORE_COUNT);
 
         // Print out the top 3 scores - you'll be binding to UI elements and want a known 
         // number of scores, so you may avoid using a loop, however, you must check to be 
         // sure you have that known number of elements, like the following:
 
         if (top3Scores.Length == TOP_SCORE_COUNT)
         {
             Debug.Log("Top scores:");

             if (m_topScoreLabel)
                 m_topScoreLabel.text = "First place: " + top3Scores[0].playerName + ", " + top3Scores[0].score  + " points";
         
             Debug.Log("First place: " + top3Scores[0].playerName + ", " + top3Scores[0].score  + " points");
             Debug.Log("Second place: " + top3Scores[1].playerName + ", " + top3Scores[1].score + " points");
             Debug.Log("Third place: " + top3Scores[2].playerName + ", " + top3Scores[2].score  + " points");
         }
 
         // Output should be:
         // Top Scores:
         // First place: Ray, 50 points
         // Second place: Billy, 40 points
         // Third place: Joe, 20 points
     }
     
     // Lets throw in a reusable bit of code to get a sorted set of top scores, using Linq
     PlayerScore[] GetHighScores(int topCount)
     {
         return m_scores.OrderBy(ps => ps.score).Take(topCount).ToArray();
     }
 }
 
 // Need some mutable way of tying a score to a player, so using a Dictionary is discouraged.
 // Structs to the rescue:
 public struct PlayerScore
 {
     public int    score;
     public string playerName;  
 }




Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image szonjabalint · Mar 06, 2019 at 09:59 PM 0
Share

Hey, your script is lifesaving, I know it's been almost 2 years but I'm trying to use your script. It works good with the test data. But I can't figure out two things. The first one is about that I need to save the data when my game is over and retrieve it in the menu's scoreboard, but I can't seem to make two scripts out of this one (one for saving, one for retrieving). The other one is, that my score is stored in a variable called calculatedScore from my CalculatedScore$$anonymous$$anager, and the user name would come through an inputfield. It's clear that it'd look like this for me: [SerializeField] InputField inputField;

 ...
 
 m_scores.Add(new PlayerScore() { playerName = inputField.text, score = CalculatedScore$$anonymous$$anager.calculatedScore });
 
 ...

But I also need a method for the inputfield's On End Edit option.

Could you help me with these if you can and if you're still around?

Thank you!!!

inputfield.jpg (68.6 kB)
avatar image Maverickthebest · Mar 14, 2019 at 04:34 PM 0
Share

Ist old but If i Try it the Apear in the Opposit Like Smalles Number First?! How can i Change it?

avatar image szonjabalint Maverickthebest · Mar 14, 2019 at 05:41 PM 0
Share

Reverse this part:

 return m_scores.OrderBy(ps => ps.score).Take(topCount).ToArray();


avatar image Maverickthebest szonjabalint · Mar 15, 2019 at 10:08 AM 0
Share

Jeahh Perfect. Thanks.! In die end i used: "OrderByDescending"

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

142 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how to save a lot of character data? 1 Answer

Vroid Opening Incorrect Save,Vroid opens the model above the one I click 0 Answers

Problem with saving player data 0 Answers

Race saving best times on different scene 0 Answers

How to stop a countdown timer and disable/stop everything in the scene? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges