• 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 donimation · Sep 23, 2016 at 03:55 PM · arraylistleaderboardscoreboardarrange

store/rearrange integers for a basic scoreboard with C#

Hello everyone,

I'm programming my fist game and I'd like to learn how to create a scoreboard with C#. For now I'd be happy to have just the local/personal top 3 working, but eventually I'd like to see a board with the top 100 players online.

Basic if statements and simple methods have managed to get me surprisingly far, but for somet$$anonymous$$ng like t$$anonymous$$s I gather that I ought to look into Arrays or possibly even Lists.

The closest example that I've been able to find so far has been in t$$anonymous$$s post here, specifically

  int [] $$anonymous$$ghScore = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  
  int InsertScore( int score ) {
     int i = 0;
     //Look for the index to insert score
     w$$anonymous$$le( i < $$anonymous$$ghScore.length ) {
        if( $$anonymous$$ghScore[i] <= score ) {
           break;
        }
        i++;
     }
     //Score doesn't make it to top 10
     if( i >= $$anonymous$$ghScore.length ) {
        return -1;
     }
     //Push all the scores not $$anonymous$$gher than score backward
     for( int j = $$anonymous$$ghScore.length - 1; j <= i; --j ) {
        $$anonymous$$ghScore[j] = $$anonymous$$ghScore[j-1];
     } 
     //Set score
     $$anonymous$$ghScore[i] = score;
     return i;
  }

Thank you @Chronos-L

I can follow t$$anonymous$$s enough to start implementing somet$$anonymous$$ng similar in my own code but before attempting t$$anonymous$$s I did a bit more research and read that "Arrays are outdated and Lists are the way to go" so to speak. I have a feeling that Lists might be slightly more flexible when it comes to arranging data partly because they don't have to have a fixed length.

Would Lists be able to handle t$$anonymous$$s task in a more elegant way, and if so how? Since I'm still struggling with syntax any links or examples you can provide on the subject would be enormously appreciated!

Thank you for taking the time to read t$$anonymous$$s and I look forward to any feedback!

Best regards, Don

Comment
Add comment · Show 1
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 EvilTak · Sep 23, 2016 at 04:52 PM 0
Share

2 Replies

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

Answer by donimation · Sep 30, 2016 at 03:32 PM

Ok, t$$anonymous$$s is my solution:

 using UnityEngine;
 using System.Collections.Generic;              // TO ENABLE THE USE OF List<>
 
 public class YoClass: MonoBehaviour
 {    public static List<int> top3list;
         public static bool sortingStopped;      // STOPS THE LOOP 
 
         void Start ()
         {    top3list = new List<int>();
         top3list.Add(0);                      // QUICK AND DIRTY WAY TO CREATE
         top3list.Add(0);                      // 3 EMPTY "SLOTS"
         top3list.Add(0);
         sortingStopped  = false;
          }
     
      void Update()
      {    SortTheList(YoGame.currentScore);
      } 
 
      void SortTheList(int score)
     {    if (sortingStopped == false)
         {    if (score > top3list[2])
             {    top3list[2] = score;
                 top3list.Sort();
                 top3list.Reverse();
             }
             sortingStopped = true; 
         }
     }
 }


sorry for the weird formatting; had some troubles with dat:|

Might not be pretty, but it gets the job done.
Thanks again @Happy-Zomby!
Only t$$anonymous$$ng I did different was to always re-assign the last index in order to keep the List count to 3.
Then I .Reverse(d) after .Sort(ing) to keep the top score at [0]

Hope t$$anonymous$$s helps anyone else looking for a "basic" score board with C#, and if you t$$anonymous$$nk of a more efficient way to do the same t$$anonymous$$ng feel free to post your solution!

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
1

Answer by Happy-Zomby · Sep 23, 2016 at 05:54 PM

$$anonymous$$, what you have should work but it is true that list would be easier... and it has functions to sort it and so on..... and you don't need to specify size

 using UnityEngine;
 using System.Collections.Generic;
 
 public class ScoreManager : MonoBehaviour {
 
     public List<int> scores = new List<int>();
     public bool test;
     public bool test2;
 
 
     public void AddScore(int score)
     {
         scores.Add(score);
         //will add your last score if you call it from somewhere else
     }
 
     public void SortScore()
     {
         scores.Sort();
     }
 
     void Update()
     {
         if(test==true)
         {
             test = false;
             AddScore(Random.Range(0, 1000));
         }
 
         if(test2 == true)
         {
             test2 = false;
             foreach (int sc in scores)
             {
                 Debug.Log(sc); 
             }
 
             SortScore();
 
             foreach (int sc2 in scores)
             {
                 Debug.Log(sc2);
             }
         }
 
     }


the update part is just so you can test it.... $$anonymous$$t test a few times and then test 2 you will get the unsorted scores in the console - followed by the sorted ones.

so if you ignore the update part you see the power of list... 3 lines only: declare add sort and there you are

You can do even more advanced stuff with list using System.Linq but no need here. Lists are great - would recommend using them,

if you want to have a name with your score... you can great a class with a string and int... then make a list of that... and use OrderBy to sort the list of name/score by score... but t$$anonymous$$s is more advanced... look for OrderBy for system.Linq for t$$anonymous$$s

hope that helps

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 donimation · Sep 29, 2016 at 06:10 PM 0
Share
avatar image donimation donimation · Sep 29, 2016 at 07:25 PM 0
Share
avatar image donimation donimation · Sep 29, 2016 at 07:41 PM 0
Share
avatar image Happy-Zomby donimation · Sep 29, 2016 at 08:55 PM 0
Share

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

61 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

Related Questions

A node in a childnode? 1 Answer

How to arrange list 3 Answers

How to combine Vector3 arrays? 3 Answers

Object not set to an instance of an object?? why? 0 Answers

2d array index out of range 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