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.