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!
@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;
}
Ist old but If i Try it the Apear in the Opposit Like Smalles Number First?! How can i Change it?