2

I am currently following a tutorial on making a game in Unity using C#. In it, they showed us how to show the score in the center of the screen using

using UnityEngine;
using UnityEngine.UI;

public class Score : MonoBehaviour
{
    public Transform player;
    public Text scoreText;

    // Update is called once per frame
    void Update()
    {
        scoreText.text = player.position.z.ToString("0");
    }
}

However, I want to go further and figure out how to make a high score list in the top left corner. My only experience is with python so I know I would go about making an empty array of size 10, and then each time the game restarts, if the score is greater than any of the highest 10 scores in the mentioned list, it is inserted in the corresponding position (keeping the elements ordered), and the lowest value is dropped from the list (to keep a list of just 10 elements). However, I am confused about the syntax of C#.

Currently, my thought process for the code (which would go on my restart statement), if it were python is this

##this is how I would write it in python I believe
array = np.zeros[10]
for i in range(0, len(array)):
    if player.position.z.ToString("0") < array[-i]
          continue
    else:
          array[-i-1] = player.position.z.ToString("0")

Where obviously the player.position statement is from c#. I was wondering if anyone could help me translate the thought process over. It seems I need to declare an array of strings before I can use it but I am not sure.

Thanks for any help

3
  • Telling you to use ToString() every frame for that is a terrible idea. I don't even know how someone thought that suggestion was even mildly appropriate. Commented Mar 6, 2019 at 3:48
  • @Krythic Oh, I am just following Brackey's tutorial on youtube, what is the substitute? Commented Mar 6, 2019 at 4:05
  • 1
    I think if you find current score is higher than any entry of the array, you would need to enter that score and other entries of the array should be shift down (rather than replacing) Commented Mar 6, 2019 at 4:45

4 Answers 4

1

As I understood you want an array with 10 elements where you store the top 10 scores. So each new score which is higher than the existing top 10 is placed in the correct position of that top 10.

Something like

current Top10 => 2, 3, 5, 7, 8, 9, 12, 15, 18, 20

newScore = 16

new Top10 => 3, 5, 7, 8, 9, 12, 15, 16, 18, 20

Option 1:

string[] scoreArray = new string[10];

//Initializing the score array
for (int i = 0; i < 10; i++)
{
    scoreArray[i] = 0; 
}

//Somewhere in your code a new score will be assigned to this variable
int newScore;

//Checking potentially higher score

boolean replaceFlag = false;
for (int i = 0; i < 10; i++)
{
    if(newScore > scoreArray[i]) 
    {
        replaceFlag = true;
    }else{
        //newScore lower than lowest stored score
        if(i == 0)
        {
            //Stop the loop
            i = 11;
        }else if(replaceFlag){

            //The rest of the elements in the array move one position back
            for(int j = 0 ; j < i-1; j++ )
            {
                scoreArray[j] = scoreArray[j+1];
            }
            //Finally we insert the new score in the correct place
            scoreArray[i-1] = newScore;         
        }
    }
}

Option 2: Using List

//Create and initialize list of scores
List<int> listScores = new List<int>{ 0,0,0,0,0,0,0,0,0,0};



// If later, at some point we have the following scores 2, 3, 5, 7, 8, 9, 12, 15, 18, 20

//When you get a new score (our list will have)
listScores.Add(newScore);

//After inserting 2, 3, 5, 7, 8, 9, 12, 15, 18, 20, 16

//Ordering the list in descending order
listScores.Sort()
//Now we have 2, 3, 5, 7, 8, 9, 12, 15, 16, 18, 20,


//Drop last element of the list to keep the fixed size of 10.
listScores.RemoveAt(0)
//Now we have 3, 5, 7, 8, 9, 12, 15, 16, 18, 20

//In case you want to have the list in descending order
listScores.Sort((a, b) => -1* a.CompareTo(b));
Sign up to request clarification or add additional context in comments.

Comments

1

if Score is the array, then add the scores to this Score array variable and then sort and reverse this array whenever u want to update the high score.

private void UpdateScore(int[] ScoreArray)
{
   Array.Sort(ScoreArray);
   Array.Reverse(ScoreArray);
} 

Comments

1

You would create a known array size of 10 like this:

string[] aStringArray = new string[10];

Also add this at top;

using System.Collections.Generic;

Arrays - C# Programming Guide

3 Comments

What is the resize comment doing? It seems like it is making it bigger every loop?
more tipically, you would use a List<string>: no need to resize, just add/remove elements to that list.
I don't tink Original poster wants to use array, he just want to do in c# what it would do with array in Pyton. Changing the language, the best suggestion is to give the right tool to do the job in the new language.Just my 2 cents
0

Array must have length, if you want unknown size then use List

List<int> player = new List<int>();
player.Add(1);

3 Comments

Hello Saif, I read the question but didn't get where he would want to use list (or array of known size).
in C# array must have length, if want to use unknown length, the use list instead
Yes, I know that. but where in the requirement it is stated that he wants unknown length array?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.