I don’t really know why Generic List are more used than Array.
But i just curious how to replace 2d array with list, for example this
private GameObject[,] playingRings;
// Use this for initialization
void Start () {
playingRings = new GameObject[5, 6];
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 6; j++)
{
//playingRings[i,j] = Instantiate something
}
}
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0))
{
playingRings[0, 0].SetActive(false);
}
}
Does anyone know how to convert GameObject[,] to List ?
just build the list…
if you want it flattened:
List<GameObject> mylist = new List<GameObject>();
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 6; j++)
{
mylist.add (playingRings[i,j]);
}
}
is that what you ment?
or maybee you ment
List<List<GameObject>> mylist;
List<List<GameObject>> mylist = new List<List<GameObject>> ();
for (int x = 0; x<6;x++)
{
mylist.Add (new List<GameObject> ());
for (int y = 0; y < 5;y++)
{
GameObject go = null; //get some game object or something
mylist[x].Add(go);
}
}
GameObject samplefromlist = mylist[2][2];
I think this here answer your original question.
If i had to guess you might be better with ArrayList depends on your application.
Advantages of Generic List/ListArray is implmentation of the interface → Search, binari Search, Sort , contains etc. with array you have to implement some of them yourself.
good luck
The code is more longer but it is easy to understand,
private List<List<GameObject>> playingRingsList = new List<List<GameObject>>();
private List<GameObject> object1 = new List<GameObject>();
private List<GameObject> object2 = new List<GameObject>();
private List<GameObject> object3 = new List<GameObject>();
private List<GameObject> object4 = new List<GameObject>();
private List<GameObject> object5 = new List<GameObject>();
private List<GameObject> object6 = new List<GameObject>();
void Start () {
for (int i = 0; i < 6; i++)
{
object1.Add(Instantiate something);
object2.Add(Instantiate something);
object3.Add(Instantiate something);
object4.Add(Instantiate something);
object5.Add(Instantiate something);
object6.Add(Instantiate something);
}
playingRingsList.Add(object1);
playingRingsList.Add(object2);
playingRingsList.Add(object3);
playingRingsList.Add(object4);
playingRingsList.Add(object5);
playingRingsList.Add(object6);
}
void Update () {
if (Input.GetMouseButtonDown(0))
{
playingRingsList[0][0].SetActive(false);
}
}
is that what you want ?