Saturday, 3 September 2016

c# - How do you add multiple elements in a list at once or with a loop?

I am trying to add a non-constant value of elements by either looping through x times or just adding x nr of elements directly.



I've tried looping for x amount of times and add an element every iteration, but it only ends up with one element. I've also tried adding a collection, but the same result occurred.




using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;



public class tileMapDepth
{
public List tileMapWidth;


public tileMapDepth(List realWidth)
{
this.tileMapWidth = realWidth;
}
}

public class TestListTypeAndSuch : MonoBehaviour
{


public List tileMap = new List(1);
public int width = 5;
public int depth = 5;

void Start()
{
for (int i = 0; i < depth; i++)
{
tileMap.Add(new tileMapDepth(new List(new List(width))));
foreach (tileMapDepth tile in tileMap)

{
for (int j = 0; j < width; j++)
{
tile.tileMapWidth.Add(false);
}
}
}
}
}



The expected result is to add a nr of elements into a list and not just one element.
When i try and add booleans with a constant value as the amount it works fine. But i need to add it with a dynamic variable. The code below is the only one that is working.



        for (int i = 0; i < depth; i++)
{
tileMap.Add(new tileMapDepth(new List(new List(new bool[13]))));
}

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...