I need to declare an Array of Lists variable in Unity. Array size is fixed and it is 5. My code is working as i want, but i think there must be a better and a shorter way to do it:
public List<GameObject>[] charactersOnBoardSortedP1 = new List<GameObject>[] {new List<GameObject>(), new List<GameObject>(), new List<GameObject>(), new List<GameObject>(), new List<GameObject>() };
I tried to do like this:
public List<GameObject>[] charactersOnBoardSortedP1 = new List<GameObject>[5];
But i have an error: Object reference not set to an instance of an object.
Is there any way to declare my variable simpler? I mean if my array has only 5 slots it's ok, but what if 100? It's kind of stupid to copy new List... 100 times in declaration.
CodePudding user response:
try this. Test here
Create an Array of list variables. initialise each Array items to a new list of GameObject. then you can add to said array's list.
using System;
using System.Text;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
// create array of lists
List<GameObject>[] COBSP1 = new List<GameObject>[5]; //charactersOnBoardSortedP1
// setup lists
COBSP1[0] = new List<GameObject>();
COBSP1[1] = new List<GameObject>();
COBSP1[2] = new List<GameObject>();
COBSP1[3] = new List<GameObject>();
COBSP1[4] = new List<GameObject>();
// add objects to said list
COBSP1[0].Add(new GameObject());
COBSP1[0].Add(new GameObject());
COBSP1[0].Add(new GameObject());
COBSP1[0].Add(new GameObject());
COBSP1[1].Add(new GameObject());
COBSP1[1].Add(new GameObject());
COBSP1[1].Add(new GameObject());
COBSP1[1].Add(new GameObject());
COBSP1[2].Add(new GameObject());
COBSP1[2].Add(new GameObject());
COBSP1[2].Add(new GameObject());
COBSP1[2].Add(new GameObject());
COBSP1[3].Add(new GameObject());
COBSP1[3].Add(new GameObject());
COBSP1[3].Add(new GameObject());
COBSP1[3].Add(new GameObject());
COBSP1[4].Add(new GameObject());
COBSP1[4].Add(new GameObject());
COBSP1[4].Add(new GameObject());
COBSP1[4].Add(new GameObject());
}
}
CodePudding user response:
try this
var arraySize=6; //or what size you need
List<GameObject>[] charactersOnBoardSortedP1 = new List<GameObject>[arraySize];
charactersOnBoardSortedP1= charactersOnBoardSortedP1
.Select(obsp => obsp = new List<GameObject>())
.ToArray();
or if you need to init some more things in the same time
for (var i=0; i< charactersOnBoardSortedP1.Length; i )
{
charactersOnBoardSortedP1[i]=new List<GameObject>();
// if you need to add some elements in the same time
charactersOnBoardSortedP1[i].Add(new GameObject {Id=i});
}
CodePudding user response:
Easiest way to achieve it is as follows:
int arraySize = 5;
List<GameObject>[] charactersOnBoardSortedP1 = Enumerable.Repeat(new List<GameObject>(), arraySize).ToArray();
This will create array of specified size filled with empty List<GameObject>()
If you wish to have list to have GameObject
you can modify above code a bit.
int arraySize = 5;
List<GameObject>[] charactersOnBoardSortedP1 = Enumerable.Repeat(new List<GameObject> { new GameObject()}, arraySize).ToArray();