Home > database >  How to Add value in 2D List
How to Add value in 2D List

Time:06-01

Why i can't add value in my 2D List? i try below but no work??

   public List<Material> Pantsmaterial;

   public List<Material[]> PantMMMM;

   private void Awake()
   {
       PantMMMM = new List<Material[]> { 
        { Pantsmaterial[0], Pantsmaterial[1] } ,
        { Pantsmaterial[0], Pantsmaterial[1] }
       };

   }

CodePudding user response:

You must explicitly create new arrays within.

PantMMMM = new List<Material[]>() {
    new Material[] { Pantsmaterial[0], Pantsmaterial[1] },
    new Material[] { Pantsmaterial[0], Pantsmaterial[1] },
};

CodePudding user response:

Initialize your list as below and read about Object and Collection Initializers for more.

private void Awake()
{
    PantMMMM = new List<Material[]>
    {
        new Material[]  { Pantsmaterial[0], Pantsmaterial[1] } ,
        new Material[]  { Pantsmaterial[0], Pantsmaterial[1] }
    };
}

CodePudding user response:

Use simply List.Add:

private void Awake()
{
    PantMMMM.Add(new Material[] {Pantsmaterial[0], Pantsmaterial[1]});
    PantMMMM.Add(new Material[] {Pantsmaterial[0], Pantsmaterial[1]});
}
  • Related