Hi @DranrebKing - @NoDumbQuestion already provided some good links. You should familiarize yourself with the concepts first - learn about:
- Arrays and Lists
- Multidimensional arrays
- Iteration of arrays
- How to read array variables
- how to spawn and remove gameobjects (or recycle them from object pool)
Note that this is no way fully working code, you should definitely first read the articles provided in comments and learn how things work.
The idea is that you create the playfield from data, then do the checking of valid moves, neighbours, removal of tiles and such using something like the CheckTileAt method here. After each frame’s updates, whatever the game logic does, you’ve done changes to your game playfield data, and you have to update “the visuals”. Here I just once instantiate gameobjects in Start, to match the playfield data. in reality, you’d simply update/remove/respawn needed block Gameobjects each frame.
// create playfield data 3x4
// tile type defined in 1,2,3 int values
public int[,] tiles =
{
{1, 3, 1 },
{1, 3, 1 },
{1, 2, 2 },
{1, 1, 2 }
};
// Prefab used to display the tile data
public GameObject blockPrefab;
void Start ()
{
// Update blocks from data
UpdateBlocks();
// How to read tiles in x,y position of array
Debug.Log("Tile at 0,0 is:" + CheckTileAt(0,0));
Debug.Log("Tile at 1,2 is:" + CheckTileAt(1,2));
Debug.Log("Tile at 2,3 is:" + CheckTileAt(2,3));
}
void UpdateBlocks ()
{
var x = 0;
var y = 0;
// Iterate all tiles of x,y array - for loop might be better
foreach (var t in tiles)
{
// Instantiate block
var block = Instantiate(blockPrefab) as GameObject;
// Set material by tile type
if (t == 1)
block.GetComponent<Renderer>().material.color = Color.red;
else if (t == 2)
block.GetComponent<Renderer>().material.color = Color.blue;
else if (t == 3)
block.GetComponent<Renderer>().material.color = Color.yellow;
// Set block position
block.transform.position = new Vector2(x,-y);
x++;
if (x == 3)
{
x = 0;
y++;
}
}
}
// Helper
int CheckTileAt (int x, int y)
{
return tiles[y,x];
}