- Home /
Multidimensional array of structs
I'm trying to use a multidimensional array of structs in C#. After some time I have managed to write a single row array without fixed length. The code is as follows:
public GameObject block;
public GameObject emptySpace;
public struct cell {
public int cellRotation;
public GameObject cellBlock;
public cell(int r, GameObject b){
cellRotation = r;
cellBlock = b;
}
}
void Start () {
cell[] map = {
new cell(90, block), new cell(180, emptySpace), new cell(270, block)
};
}
I can access the structs' properties easily by doing something like map[0].cellRotation
My question is, how can I define a new multidimensional array of structs of fixed length (say [2,3]) and how could I access it later on? Every single thing I've tried returns an error.
Thanks
Answer by Kryptos · May 15, 2012 at 02:27 PM
C# documentation about multidimensional arrays: http://msdn.microsoft.com/en-us/library/2yd9wwz4%28v=vs.100%29.aspx
Thanks Nicolas $$anonymous$$usset. I don't know why it refused to compile before, but I can write a second row now. It still returns an error when I try to specify its length, though.
Answer by Agemoi · May 15, 2012 at 10:08 PM
Finally got it to work. I'm sorry for wasting your time. In case anyone else encounters a similar problem it looks like this:
cell[,] map = new cell[2, 3]{
{new cell(90, block), new cell(180, emptySpace), new cell(270, block)},
{new cell(90, emptySpace), new cell(180, block), new cell(270, emptySpace)}
};
You would have had to set the struct with the constructor inside first.
Thanks everyone