Unlimited WordPress themes, graphics, videos & courses! Unlimited asset downloads! From $16.50/m
  1. Game Development
  2. Complete Games

Build a Grid-Based Puzzle Game Like Minesweeper in Unity: Interaction

This post is part of a series called Build a Grid-Based Puzzle Game Like Minesweeper in Unity.
Build a Grid-Based Puzzle Game Like Minesweeper in Unity: Setup
Build a Grid-Based Puzzle Game Like Minesweeper in Unity: Winning

We continue building our grid-based puzzle-game by connecting the tiles to each other, making them light up with the mouse cursor, and adding the ability to place flags.

In the last part of this tutorial, we created a field of tiles which form the basis for our puzzle game. In this part, we'll make it playable. This tutorial follows on directly from the last part, so read that before starting this.

Lighting Tiles Up on Mouseover

When the mouse is over a tile, we want it to light up. This is a cool feature which gives even simple actions (such as moving your mouse pointer) instantaneous feedback.

We use the OnMouseOver() function to accomplish this. It gets called automatically whenever the mouse cursor moves over the object the code is attached to. Add these variables to the Tile script:

Then assign your basic tile material to the materialIdle slot. We also need a lightup material, which should have the same color, but uses a different shader. While the basic material can have a diffuse shader...

...the lightup material could have a specular shader. Many games use an additional rim shader for that effect too. Those don't come with Unity, but if you can figure out how to get one, you can use that instead!

Don't forget to actually assign the materials to the Material slots on the tile prefab, so that they can be used.

Then add this OnMouseOver() function to the Tile script

Try it out! When you move the mouse cursor over the tiles, they should change their appearance.

What you might have noticed is that the tiles change their appearance once the mouse is over them, but doesn't actually change back. For that, we need to use the OnMouseExit() function:

And voilá; we now have tiles that light up and make the game much more interesting.

Assigning IDs to Tiles

In order to have the tiles communicate with each other (to find out how many mines are nearby), each tile needs to know its neighboring tiles. One way to accomplish that is using IDs, which each tile will be assigned.

Start by adapting the Tile code to include an ID variable. Also, add a variable to hold the number of tiles per row, which we'll use in this computation:

Then modify the instantiate command in the Grid code to look like the following snippet. (The new line assigns IDs to the tiles as they are being created.)

The first tile will get the ID 0, the next one will be assigned the ID 1, and so on. You can check them by clicking the tiles during runtime, and seeing what number they have been assigned.

Getting Neighboring Tiles

Now we want each tile to know about its neighboring tiles. When we execute an action on a tile (such as uncovering it), we need to take the neighboring tiles into account.

In our case, this means counting the mines that are adjacent to the tile we just uncovered, and possibly uncovering other tiles as well—but we'll get to that later.

This can also be used to, say, check whether three or more tiles are next to each other in a Match-3 game.

Start by adding these variables to the Tile script:

These will hold all the neighboring tiles. They are public so that we can check during runtime that they have actually been assigned correctly.

Since now every Tile has an ID, the number of tiles that appear in a column, and access to the static array that has all tiles saved in the Grid class, we can calculate the positions of the neighboring tiles after they have been created.

That part looks like this:

With the IDs and the number of tiles per row, we can calculate which tiles are nearby. Suppose the tile doing the calculations has the ID 3, and that there are five tiles per row. The tile above it will have the ID 8 (the selected tile's ID plus the number of tiles per row); the tile to the right will have the ID 6 (the selected tile's ID plus one), and so on.

Unfortunately, this isn't enough. The code correctly checks the numbers, but when it asks the allTiles array to return the tiles, it can request index numbers that are out of range, producing a long list of errors. 

In order to fix this, we need to check that the index we request from the array is actually valid. The most efficient way to do this is with a new inBounds() function. Add it to the Tile:

Now we need to check that each possible neighboring tile is within the bounds of the array that holds all the tiles, before we actually try to get it from the array:

That code block checks all the possibilities. It also checks whether a tile is at the edge of the field. A tile on the right-hand edge of the grid, after all, does not actually have any tiles to its right.

Try it out! Check a few tiles and see whether you've retrieved all the neighboring tiles correctly. It should look like this:

As the tile in this screenshot is a tile on the right edge of the field, it has no neighbors to the right, upper right, or lower right. The variables without assigned tiles are correctly empty, and a test reveals that the remaining ones to have been assigned correctly too.

Finally, once we've made sure that this works, we add all neighboring tiles into an array, so that we can access them all at once later. We need to declare this array at the beginning:

You can then adapt each line of the algorithm we created above to enter each neighboring tile in that array, or add this block afterwards:

Counting Nearby Mines

After all tiles have been created, all mines have been assigned, and each tile has retrieved its neighbors, we then need to check whether each of these neighboring tiles is mined or not.

The Grid code already assigns the specified number of mines to randomly chosen tiles. Now we only need each tile to check its neighbors. Add this code at the beginning of the Tile script, so that we have a place to store the amount of mines:

To count them, we run through the array into which we previously added all neighboring tiles, and check each entry in turn to see if it is mined. If so, we increase the value of adjacentMines by 1.

This function also sets the text element of the tile to display the number of mines nearby. If there are no mines, it displays nothing (rather than 0).

Tracking the State of Each Mine

Let's add a state to each tile. This way, we can keep track of which state it is currently in—idle, uncovered, or flagged. Depending on which state the tile is in, it will react differently. Add it now, as we'll use it in a moment.

Adding Flags

We want to be able to mark tiles as flagged. A flagged tile has a tiny little flag on top of it. If we right-click the flag it will disappear again. If all mined tiles have been flagged, and no incorrectly flagged tiles remain, the game is won.

Start by creating a flag object, and add it to the tile (you'll find a flag mesh in the source files).

We also need a variable to access the flag. Add this code:

Remember to drag the flag that is part of the tile onto the displayFlag slot.

Also, add this to the start() function of the tile:

This will disable the flag and text at the beginning. Later, we can then activate a flag, making it visible again, effectively putting it there. Alternatively, if we uncover a tile, we make the text visible again.

Placing and Removing Flags

We'll write a function that handles both placing and removing flags:

Once you've added that, also add the code to handle a click event. To do this, adapt the OnMouseOver() function we already have to check for a mouse click:

This will recognize a right-click (button 1) and activate the SetFlag() function. It will then either activate or deactivate the flag on the current tile. Try it out!


We've extended our puzzle game with several vital features, made it visually more interesting, and have given the player the ability to affect the playing field.

Next time, we'll add uncovering of tiles, make a simple interface, and turn this into a proper game.

Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.