jagged array for top 10 scores

I’m trying to make an array for the top 10 scores (worldwide), which has to be read from a on-line database. The database part is no problem, but making the array to get it working is :frowning:

The array declaration I want to make is (taken from some ‘manual’):

	int[][] myWorldScores = new int[4][];
	myWorldScores[0] = new string[10]; // Name
	myWorldScores[1] = new int[10]; // High score
	myWorldScores[2] = new byte[10]; // Platform
	myWorldScores[3] = new string[10]; // Date

but each line gives me 4 errors though:

Assets/Scripts/MainGame.cs(17,9): error CS0178: Invalid rank specifier: expected ,' or ]’
Assets/Scripts/MainGame.cs(17,26): error CS1519: Unexpected symbol =' in class, struct, or interface member declaration* *Assets/Scripts/MainGame.cs(17,32): error CS0178: Invalid rank specifier: expected ,’ or ]'* *Assets/Scripts/MainGame.cs(17,42): error CS1519: Unexpected symbol ;’ in class, struct, or interface member declaration

I’m kinda at a loss how to setup jagged arrays in C#/Unity. I’ve made lots of these in the past in other (non-OOP) languages without problems.

Any help is appreciated!

Jagged Array reference: Arrays - C# reference | Microsoft Learn

I think you are missing a value in your first line.
You want to initialize an array of 4x10, but you only write new int [4] [ ].
Try with new int [4] [10].
But then you have an array of 4x10 integers. And you try to assign strings and bytes to it. That can not work.

You should use a class for it:
Build a class with all your needed values and then make an array of those class.
Then you have 10 “packages” with all your values in (“WorldScores” in the following code)

public class myWorldScores {

string myWorldScoreName;
int myWorldScoreScore;
byte myWorldScorePlatform;
string myWorldScoreDate;

}

public myWorldScores[] WorldScores = new myWorldScores[10];

Did you read the page and check my code? I’ve done exactly what’s written there and yet I get errors :frowning:

Nice thinking, but adding new int[4][10] gives me a new error:
Assets/Scripts/MainGame.cs(16,46): error CS0178: Invalid rank specifier: expected ,' or ]’

You are getting errors because you are mixing types here. The jagged array you defined is a matrix of integers, meaning you can only store ints in there. But you are trying to cram strings and bytes into the array.

If you want to go with a jagged array you could store the data in strings:

string[][] myWorldScores = new string[4][];
myWorldScores[0] = new string[10]; // Name
myWorldScores[1] = new string[10]; // High score
myWorldScores[2] = new string[10]; // Platform
myWorldScores[3] = new string[10]; // Date

Or do something like this:

public class WorldScore
{
    public string name { get; set; }
    public string date { get; set; }
    public int highScore { get; set; }
    public byte platform { get; set; }
}

WorldScore[] myWorldScores = new WorldScore[ 10 ];

Thanks for the help! Going for the class WorldScore I think because I also want to use a personal top-3 score as well :wink:

Kinda odd though that a high language doesn’t support jagged arrays with mixed value types while other (lower) languages do. Oh well, learned an other thing today :wink:

Sorry Reizla, I’m tired and posted an unhelpful reply with only a link.

Glad you got it sorted though.

Hope I didn’t sound too harsh in my reply to you, if so, it wasn’t my intention but was much rather frustration that the whole thing wasn’t working :wink: Thanks for trying to help think of a solution <3