I’m experimenting with grids and 2d arrays, and I have a class that handles visuals.
in Start() I use 2 for loops for creating the tiles and it works fine.
for (int x = 0; x < gridDimentions_x; x++)
{
for (int y = 0; y < gridDimentions_y; y++)
{
tiles[x, y] = new GameObject("Tile", components).GetComponent<SpriteRenderer>();
SpriteRenderer renderer = tiles[x, y];
Debug.Log(x.ToString() + ", " + y.ToString());
renderer.transform.position = grid.GetOrigin() + grid.GetCellSize() * new Vector3(x, y);
renderer.sprite = sprites[grid.GetValue(x, y).Tile];
renderer.flipY = true;
renderer.transform.localScale = scale * Vector3.one;
renderer.transform.parent = TileMap.transform;
}
}
Then I have a function called when the grid changes that should update the tiles with the new values, but the same for loops run only one time.
void UpdateVisuals()
{
for (int x = 0; x < gridDimentions_x; x++)
{
for (int y = 0; y < gridDimentions_y; y++)
{
TileType currentTile = grid.GetValue(x, y).Tile;
Debug.Log($"{x}, {y}");
if (sprites[currentTile] == tiles[x, y].sprite)
return;
tiles[x, y].sprite = sprites[currentTile];
}
}
}
The console only shows 0, 0
Also there aren’t errors in the console.