List of List of Booleans function giving weird results

Hello there! I’m attempting to create a script that creates a 2D list of Boolean values. This ‘grid’ will be used for determining whether a certain position can hold an object or not. True for yes, false for no. The one stipulation is that the position cannot be true if the column above it is true. If the position above is false, we use an RNG to determine if it gets marked true or stays false. Here’s an example:

[T, F, F, T, F]
[F, F, T, F, T]
[F, T, F, T, F]
... and so on

I wrote the following script to accomplish this:

    private int _gridSize;
    private List<List<bool>> _grid;

    void Awake()
    {
        //Set up size of grid and initialize the array
        _gridSize = 5;
        ResetGrid();
        //Pass a random first entry to kick things off
        GenerateGridPositions( new List<bool>() { true, false, true, true, false } );
    }

    void Update()
    {
        //Save last row of grid
        var lastRow = _grid[4];
        //Set all positions to false
        ResetGrid();
        //Generate based on saved row
        GenerateGridPositions(lastRow);
        //For Debug purposes
        for (int i = 0; i < _gridSize; i++)
        {
            Debug.Log(i + ":" + String.Join(",", _grid*.Select(x=>x.ToString()).ToArray()));*

}
}

private void ResetGrid()
{
_grid = new List<List>();
for (int i = 0; i < _gridSize; i++)
{
_grid.Add(new List());
}
}

///


/// Sets the positions of the obstacles for the next street. Columns should not have obstacles in
/// two successive rows. Probably should go into the obstacle manager.
///

private void GenerateGridPositions(List lastRow)
{

//Set the first row based on the row passed
for (int i = 0; i < _gridSize; i++)
{
if (!lastRow*)*
{
_grid[0].Add(PositionChance());
}
else
{
_grid[0].Add(false);
}
}
//Set each subsequent row, based on the row positions above it
for (int i = 1; i < _gridSize; i++)
{
for (int j = 0; j < _gridSize; j++)
{
if (!_grid[i-1][j])
{
grid*.Add(PositionChance());*
}_

else
{
grid*.Add(false);*
}
}
}
}_

///


/// Determines whether an obstacle is added to the grid. Potentially can be skewed to
/// produce trues more often if there is a difficulty setting.
///

private bool PositionChance()
{
var rand = Random.Range(0, 10);
var choice = rand%2 == 0;
return choice;

}
But it does something weird upon attaching VS’s debugger to the Unity process and setting breakpoints: I’ve found that when it attempts to add the next position to a row, it adds that position to all the rows. I’m not quite sure why it does this and I’m completely at a loss. I’ve also implemented this code in Python just to test it and it works exactly as intended. Does anyone see anything I’m not seeing?
Also of note: this function wouldn’t normally be called every frame in Update(). I just have it in there for testing purposes.
Edit 2: Updated to reflect accepted answer.

Your problem ist this piece of code:

_grid = new List<List<bool>>();
_grid.AddRange(Enumerable.Repeat(new List<bool>(), _gridSize).ToList());

This will add the same inner List instance multiple times to your outer List. You only create one outer list and one inner list. So each “row” in your outer list references the same inner list.

You should initialize your grid like this:

_grid = new List<List<bool>>();
for(int i = 0; i < _gridSize; i++)
   _grid.Add(new List<bool>());