I need to instantiate an array of 2 elements that is floors of black and white color in an alternating pattern so it looks like a chess board. It is for my game that has 2D Dungeon Generation.
This is my current code:
public enum TileType
{
Wall, Floor,
}
public GameObject[] floorTiles;
public GameObject[] wallTiles;
public GameObject[] outerWallTiles;
private TileType[][] tiles;
private Room[] rooms;
private Corridor[] corridors;
private GameObject boardHolder;
void InstantiateTiles()
{
for (int i = 0; i < tiles.Length; i++)
{
for (int j = 0; j < tiles[i].Length; j++)
{
InstantiateFromArray(floorTiles, i, j);
if (tiles[i][j] == TileType.Wall)
{
InstantiateFromArray(wallTiles, i, j);
}
}
}
}
void InstantiateFromArray(GameObject[] prefabs, float xCoord, float yCoord)
{
int randomIndex = Random.Range(0, prefabs.Length);
Vector3 position = new Vector3(xCoord, yCoord, 0f);
GameObject tileInstance = Instantiate(prefabs[randomIndex], position, Quaternion.identity) as GameObject;
tileInstance.transform.parent = boardHolder.transform;
}
}
So this is how it looks right now (just random positions of floor prefabs):
And this is what I am trying to achieve (alternating):