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 /
avatar image
0
Question by jamesAboy19 · Nov 29, 2017 at 01:23 AM · arrayarrayshighscoresarraylisthighscore

Unity C# Highscore table

Hi all,

So first post, I'm not very good at all this and currently working on my first small project (Endless runner)

So I have the the players score save when they die using Player.pref and it iwll only save a score which is higher than the previous one.

What i want to do is make the score save into an array (maximum size of either 3 or 5) and then display those scores in order descending.

Can anyone help me out with this? Can post the current scripts when I get home later

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Hellium · Nov 30, 2017 at 10:20 PM

While my solution may be a little bit "too much", at least, it works ! :D

First of all, create a script called HighScoreManager.cs and put the following script in it :

 using System.IO;
 using UnityEngine;
 
 [CreateAssetMenu(menuName = "ScriptableObjects/HighScoreManager", order = 1)]
 public class HighScoreManager : ScriptableObject
 {
     private string filePath;
 
     [SerializeField]
     private string fileName = "highScores" ;
 
     [SerializeField]
     private int highScoresCount = 5;
     
     [SerializeField]
     private int[] highScores;
 
     private string FilePath
     {
         get
         {
             if ( string.IsNullOrEmpty( filePath ) )
                 filePath = Path.Combine( Application.persistentDataPath, fileName );
             return filePath;
         }
     }
 
     public int HighScoresCount
     {
         get { return highScoresCount; }
     }
 
     public float this[int i]
     {
         get { return highScores[i]; }
     }
 
     public void SubmitHighScore( int highScore = 1000 )
     {
         if( highScores == null )
             highScores = new int[HighScoresCount];
 
         int newHighScoreIndex = -1;
         for ( int i = 0 ; i < highScores.Length ; i++ )
         {
             if( highScore >= highScores[i] )
             {
                 newHighScoreIndex = i;
                 break;
             }
         }
         if( newHighScoreIndex >= 0 )
         {
             for ( int i = highScores.Length - 1 ; i > newHighScoreIndex ; i-- )
             {
                 Debug.Log( highScores[i] );
                 Debug.Log( highScores[i-1] );
                 highScores[i] = highScores[i - 1];
             }
             highScores[newHighScoreIndex] = highScore;
         }
     }
 
     public void Save()
     {
         File.WriteAllText( FilePath, JsonUtility.ToJson( this ) );
     }
 
     public void Load()
     {
         string path = Path.Combine( Application.persistentDataPath, "highScores" );
 
         if ( File.Exists( FilePath ) )
         {
             JsonUtility.FromJsonOverwrite( File.ReadAllText( FilePath ), this );
         }
         if ( highScores.Length < highScoresCount )
             highScores = new int[highScoresCount];
     }
 }


Then, in the Project view, in Unity, right click and click on ScriptableObjects > HighScoreManager. You now have an object responsible for managing your highscores, and you will be able to submit a new highscore, save and load your highscores.

Here is a dummy script to test it :

 using UnityEngine;
 
 public class NewBehaviourScript : MonoBehaviour {
 
     public HighScoreManager highScoreManager ;
 
     private void Start()
     {
         highScoreManager.Load();
 
         for ( int i = 0 ; i < highScoreManager.HighScoresCount ; i++ )
         {
             Debug.Log( highScoreManager[i] );
         }
     }
 
     private void Update()
     {
         if ( Input.GetKeyUp( KeyCode.Keypad1 ) )
             highScoreManager.SubmitHighScore( 10 );
         if ( Input.GetKeyUp( KeyCode.Keypad2 ) )
             highScoreManager.SubmitHighScore( 20 );
         if ( Input.GetKeyUp( KeyCode.Keypad3 ) )
             highScoreManager.SubmitHighScore( 30 );
     }
 
     private void OnDestroy()
     {
         highScoreManager.Save();
     }
 }


Comment
Add comment · 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
0

Answer by text23d · Nov 30, 2017 at 08:00 PM

int[] scoreArray; //this would be array of your high scores

Comment
Add comment · 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

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

83 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

Related Questions

How to Clone Array1 into Array2 in Unity? Using JavaScript. 0 Answers

How can you do calculations on two lists? 1 Answer

Linear Interpolation of Array Values 1 Answer

Get all GameObjects by variable value 2 Answers

Help with for loop and arrays 3 Answers


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