I am creating a match three style game. Currently, at the start of the game, all the tiles are filled with gems. I would like to limit the number of tiles that spawn at the start to a certain number. I have been trying for hours I can not figure a solution any help would be appreciated. Here is the code before trying to implement this limitation.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Board : MonoBehaviour
{
public int width;
public int height;
public GameObject bgTilePrefab;
public Gem[] gems;
public Gem[,] allGems;
// Start is called before the first frame update
void Start()
{
allGems = new Gem[width, height];
Setup();
}
private void Update()
{
}
private void Setup()
{
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
Vector2 pos = new Vector2(x, y);
GameObject bgTile = Instantiate(bgTilePrefab, pos, Quaternion.identity);
bgTile.transform.parent = transform; //is this necessary?
bgTile.name = "BG Tile - " + x + ", " + y;
int gemToUse = Random.Range(0, gems.Length);
SpawnGem(new Vector2Int(x, y), gems[gemToUse]);
}
}
}
private void SpawnGem(Vector2Int pos, Gem gemToSpawn)
{
{
Gem gem = Instantiate(gemToSpawn, new Vector3(pos.x, pos.y, 0f), Quaternion.identity);
gem.transform.parent = transform;
gem.name = "Gem - " + pos.x + ", " + pos.y;
allGems[pos.x, pos.y] = gem;
gem.SetupGem(pos, this);
}
}
}