- Home /
How to create a 2d array of a class
Hi, I am having trouble creating a 2 dimensional array of a class. I have found a few posts on this subject on unity answers and tried to mimic the code in them but to no avail. Here are the 4 posts that I have found. first post, second post, third post, forth post. I noticed this line of code in 2 of the posts...
T`ileInfo[,] map = new TileInfo[width,height];`
public BlockIDClass [,] gridBlocksID = new BlockIDClass [10,10];
and in a third post they omitted the "," in the [].
private MyEvent[] myEventList = new MyEvent[10] ;
Is there a reson to the ",". What does it do?
here was my test code for a 1d and a 2d array of a class.
this was the 1d array and error.
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Classtest : MonoBehaviour {
public class Stuff
{
public int bullets;
public int grenades;
public int rockets;
}
public Stuff [,] myStuff = new Stuff[1];
this was the error after saving script.
Assets/scripts/Classtest.cs(17,48): error CS0029: Cannot implicitly convert type Classtest.Stuff[]' to
Classtest.Stuff[,]'
and the code for the 2d array and error.
using UnityEngine; using System.Collections; [System.Serializable]
public class Classtest : MonoBehaviour {
public class Stuff
{
public int bullets;
public int grenades;
public int rockets;
}
public Stuff [,] myStuff = new Stuff[1][1];
this was the error for the 2d array.
Assets/scripts/Classtest.cs(17,50): error CS0178: Invalid rank specifier: expected ,' or
]'
Reading about the subject it seems a lot of people suggest using a generic list. I am creating a game that has a grid of "gamemats" as gameobjects like a chess, or checker board, and I use a 2d array to reference each of them so another 2d array would be ideal for me because of the "posx", "posy" variables I use to reference them. I don't see how using a list would work as well for me in this situation. Any help on correct syntax or what I did wrong would be very appreciated.
thank you.
Answer by Cherno · Jan 06, 2015 at 02:04 PM
Ok, first of all: The official MSDN documentation is always a good start for researching non-Unity-specific C# coding stuff:
Multidimensional Arrays (C# Programming Guide)
Is there a reson to the ",". What does it do?
the "," just indicates that there is more than one dimension to an array. In the example code you posted, it seems like there is an error in the syntax.
A very handy guide to arrays, lists and similar is this one here, highly recommended:
Which Kind Of Array Or Collection Should I Use?
A one-dimensional array is constructed like this:
int[] intArray = new int[10];//creates an array of 10 int elements
A two-dimensional array is created like this:
int[,] intArray = new int[2,4];//creates a two-dimensional array that has a length of 2 in the first dimension and 4 in the second dimension. The dimensions could be thought of as the rows and columns of a spreadsheet.
Note that the size of a one-dimensional array is accessed with
intArray.Length
, a multi-dimensional array has multiple sizes, one for each dimension, and they are accessed with
intArray.GetLength(0)
for the Length of the first dimension,
intArray.GetLength(1)
for the second dimension, and so on.
Finally, for your example, you would construct your multi-dimensional array like this:
public class Stuff
{
public int bullets;
public int grenades;
public int rockets;
}
public Stuff [,] myStuff = new Stuff[1,1];
//this would create a two-dimensional array of your Stuff class with 1 rows and 1 columns... Which wouldn't make much sense because it could only hold as much data as a normal non-array variable.
If you would like to create a larger 2d array, you can declare and access it like this:
public Stuff [,] myStuff = new Stuff[2,3];
//creates a 2d array with 2 colums and 3 rows.
//access the contents like this:
int myBullets = myStuff[1,2].bullets;
Well first let me thank you in your response and answer. It took me copying and pasting your line of code right under $$anonymous$$e and comparing them character for character, space for space to see the difference.
public Stuff [,] myStuff = new Stuff[1,1];
public Stuff [,] myStuff = new Stuff[1][1];
It's so plain to see it here. I can't believe how long this took me to figure out and even see the difference. I liked C# for coding in unity because it is so close to C++. Which was my first language I learned to code in. I seriously did not see that comma between the 2 element numbers. I am so used to the whole double brackets [1][1].
So thank you! On another note your link to the msdn documentation was in German, I don't speak German, but I did find the English version. Very nice source. I had also had read the array guide a few week earlier. Again very informative. So again thank you.
Glad to be of help. Sorry about the non-english language link, for me it showed as english (which I prefer myself even though I'm a native German speaker) because it automatically switches to the German $$anonymous$$SDN site but with optional English translation. I guess I forgot that little detail :) I edited my answer with the correct link so other users won't have the same problem :)