Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 kiwi koder · Apr 05, 2013 at 02:33 AM · array

Add to High Score Array

I have an array with the top 10 high scores. I know how to add to the array, and sort the array with Sort(); but how do you add a value to the array, lets say that is the 2nd highest score, so I want to add this new 2nd place high score, deleting the current 10th high score and moving the other scores down one place?

Cheers for any ideas

Comment
Add comment · Show 3
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 EliteMossy · Apr 05, 2013 at 02:49 AM 0
Share

Well, i would just put them in the array in any order, and then sort the list by the highest value. Infact i would use a generic list.

avatar image HunterKrech · Apr 05, 2013 at 02:50 AM 0
Share

Arrays differ in unity greatly between c# and js, which are you using?

avatar image kiwi koder · Apr 05, 2013 at 03:16 AM 0
Share

Sorry for those who posted a c# answer, I am using js

5 Replies

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

Answer by whebert · Apr 05, 2013 at 02:52 AM

Use generic lists instead. With lists you can Add, Insert, RemoveAt, etc.

C#

 using System.Collections.Generic
 
 List<int> scores = new List<int>();
 scores.Add(25);
 scores.Add(50);
 scores.Insert(0, 28);
 scores.Sort();
 scores.RemoveAt(0);


javascript

 import System.Collections.Generic;
 
 var scores : List.<int> = new List.<int>();
 scores.Add(25);
 scores.Add(50);
 scores.Insert(0, 28);
 scores.Sort();
 scores.RemoveAt(0);
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 Chronos-L · Apr 05, 2013 at 03:10 AM

This is a very basic programming exercise.

C#

 int [] highScore = 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
    while( i < highScore.length ) {
       if( highScore[i] <= score ) {
          break;
       }
       i++;
    }

    //Score doesn't make it to top 10
    if( i >= highScore.length ) {
       return -1;
    }

    //Push all the scores not higher than score backward
    for( int j = highScore.length - 1; j <= i; --j ) {
       highScore[j] = highScore[j-1];
    } 

    //Set score
    highScore[i] = score;
    return i;
 }
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 fhxgkgdkdjkfzk · Jun 26, 2016 at 06:02 AM

Correction for Chronos-L Answer. [JS version ,, but I guess its correct in C# too].

  function recordHighScore(score) {
     var i = 0;
     while(i < highScores.length) {
         if(highScores[i] <= score) 
             break;
         i++;
     }

     if(i >= highScores.length)
         return;

     for(var j = highScores.length - 1; j > i; --j) {
         console.log(highScores[j - 1]);
        highScores[j] = highScores[j - 1];
     } 

     highScores[i] = score;
 }
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 fhxgkgdkdjkfzk · Jun 26, 2016 at 06:02 AM

Correction for Chronos-L Answer. [JS version ,, but I guess its correct in C# too].

  function recordHighScore(score) {
     var i = 0;
     while(i < highScores.length) {
         if(highScores[i] <= score) 
             break;
         i++;
     }

     if(i >= highScores.length)
         return;

     for(var j = highScores.length - 1; j > i; --j) {
         console.log(highScores[j - 1]);
        highScores[j] = highScores[j - 1];
     } 

     highScores[i] = score;
 }
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 fhxgkgdkdjkfzk · Jun 26, 2016 at 06:02 AM

  function recordHighScore(score) {
     var i = 0;
     while(i < highScores.length) {
         if(highScores[i] <= score) 
             break;
         i++;
     }

     if(i >= highScores.length)
         return;

     for(var j = highScores.length - 1; j > i; --j) {
         console.log(highScores[j - 1]);
        highScores[j] = highScores[j - 1];
     } 

     highScores[i] = score;
 }
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

14 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

Related Questions

Enemy Wave 1 Answer

Playing multiple sounds on same key input ("e") 1 Answer

Can't add GameObjects to ArrayList 1 Answer

Adding an object to an array of custom objects (JS) 1 Answer

Array of GameObjects - JS 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