Sorry for the messy title, I'm not entirely sure how to articulate it.
I'm trying to use a foreach loop for a script in my unity project for a sort of line-generator on a grid. Essentially, I have an origin point on the grid, and I want all of the points that have either the same X-axis or the same Y-axis, so I end up with a cross shape. Here's a rough draft of what I've come up with:
public void MarkStraightTiles(Cell cell)
{
neighbourCells = Origin.GetNeighbours(allCells);
foreach(var neighbour in neighbourCells)
{
if(neighbour.OffsetCoord.x == Origin.OffsetCoord.x || neighbour.OffsetCoord.y == Origin.OffsetCoord.y)
{
neighbourCells.AddRange(neighbour.GetNeighbours(allCells));
}
}
}
For reference, GetNeighbours just returns a list of the four points around Origin from allCells. OffsetCoord is just the position of the point. In theory, I want it so that for every Origin neighbour, I get the neighbour of the neighbour, and repeat until I have every point with either the same X axis or Y axis. Obviously the code above doesn't work, and I don't know how to repeat the foreach loop with every new neighbour I get. I can't modify the foreach loop while it's running, it throw up an error. Any ideas on this?