Some background info on me and what I’m doing before I get to the problem. I would consider myself somewhere in between beginner and intermediate level programmer as I understand every programming concept but have problems when putting it into practice. I’m in the process of making a Match 3 puzzle game. I followed some tutorials and have an actual working game (with some kinks).
On to the problem:
I used a nested for loop to instantiate a grid with GameObjects (gems).
for (int y=0; y<GridHeight; y++) {
for (int x=0; x<GridWidth; x++) {
// Instantiate the GameObject with prefabs
GameObject g = Instantiate (gemPrefab, new Vector3(x,y,0), Quaternion.identity) as GameObject;
g.transform.parent = gameObject.transform;
Gems.Add (g.GetComponent<gem>());
}
}
The GridHeight & GridWidth are public variables so that I could have grids in varying sizes. The gemPrefab consists of a random list of materials that changes the gem different colors). The problem is that this code produces an endless list of random gems which I do not want. I want to be able to add puzzle elements to the grid such as making holes/blocks within the grid that block the gems from falling. I am completely stuck on how to do this.
One way I thought of possibly doing this is by making a 2D array with certain indexes instantiated with my “Blocks”, then having a nested for loop to instantiate the null/empty indexes with my gems.
GameObject g = Instantiate (blockPrefab, new Vector3 (1, 3, 0), Quaternion.identity) as GameObject;
g.transform.parent = gameObject.transform;
gems.Add(g.GetComponent<Gem>());
However, when I instantiate my 2D array’s index with my blocks and then do the nested for loops, it does not instantiate the rest of the loop and I get the one block GameObject floating on the grid.
In a sense, I could instantiate my original “grid” to include my blocks, but that would place them at random places (defeats the purpose of creating a puzzle mechanic). I’ve looked into the many different frameworks available on the asset store but I much rather program it myself (with some help of course). Could someone shine a little light on what I’m doing wrong?