- Home /
How do I create roadblocks in my Match 3 puzzle gameboard?
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?
Answer by Owen-Reynolds · Dec 23, 2014 at 07:15 PM
The standard way to make a grid game is to have a "backing" 2D array. Then to think of the stuff the player sees as merely a picture of it. Old idea, and you can probably find how to work with 2D game grids in much better places than Unity. Unity is only at the "now show the player" step.
Ex (illegal code, to shorten it):
Transform[,] GridTs = new Transform[10,10];
// hold the actual items (holes, gems...at each pos)
int[,] Grid = new int[10,10];
// code for what's there
// 0=nothing yet, -1=obstacle; 1-8=gem of that color
// or use a class, or enum ... but integers work just fine.
The hole-making code can place -1's where-ever. Then the gem-making code checks Grid[x,y]
and skips -1. The key is, whenever you want to know what's in a space, don't look in the gameWorld. Check Grid[x,y]
.
Last step is to go through the created grid and Instantiate what each numbers says to.
Answer by realclues · Oct 25, 2016 at 05:34 AM
DimensionXYZ how do you apply the given code by Owen-Reynolds in your project? I used the same code as you.
Your answer
![](https://koobas.hobune.stream/wayback/20220613174741im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to only show objects in range ? 2 Answers
How to debug the object position in the grid of objects? 0 Answers
Distribute terrain in zones 3 Answers