How to Make a Match 3 Game in Unity
Learn how to make a Match 3 game in this Unity tutorial! By Jeff Fisher.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
How to Make a Match 3 Game in Unity
30 mins
Combos
By re-checking all the tiles after a match is found, you'll be able to locate any possible combos that may have been created during the shifting process.
Open up BoardManager.cs and find the FindNullTiles()
method.
Add the following for
at the bottom of the method, below the for loops:
for (int x = 0; x < xSize; x++) {
for (int y = 0; y < ySize; y++) {
tiles[x, y].GetComponent<Tile>().ClearAllMatches();
}
}
After all of this hard work, it’s time to make sure everything works as intended.
Save your work and Run the game. Start swapping tiles and watch the endless supply of new tiles keep the board filled as you play.
Moving the Counter and Keeping Score
It's time keep track of the player’s moves and their score.
Open GUIManager.cs located under Scripts\Managers in your favorite code editor. This script handles the UI aspects of the game, including the move counter and score keeper.
To get started add this variable to the top of the file, below private int score;
:
private int moveCounter;
Now add this at the top of the Awake()
method to initialize the number of moves the player can do:
moveCounter = 60;
moveCounterTxt.text = moveCounter.ToString();
Now you need to encapsulate both integers so you can update the UI Text every time you update the value. Add the following code right above the Awake()
method:
public int Score {
get {
return score;
}
set {
score = value;
scoreTxt.text = score.ToString();
}
}
public int MoveCounter {
get {
return moveCounter;
}
set {
moveCounter = value;
moveCounterTxt.text = moveCounter.ToString();
}
}
These will make sure that every time the variables Score
or MoveCounter
are changed, the text components representing them get updated as well. You could've put the text updating in an Update()
method, but doing it this way is much better for performance, especially as it involves handling strings.
Time to start adding points and tracking moves! Every time the player clears a tile they will be rewarded with some points.
Save this script and open up BoardManager.cs. Add the following to the ShiftTilesDown
method, right above yield return new WaitForSeconds(shiftDelay);
:
GUIManager.instance.Score += 50;
This will increase the score every time an empty tile is found.
Over in Tile.cs, add the following line below SFXManager.instance.PlaySFX(Clip.Swap);
in the ClearAllMatches
method:
GUIManager.instance.MoveCounter--;
This will decrement MoveCounter
every time a sprite is swapped.
Save your work and test out if the move and score counters are working correctly. Each move should substract the move counter and each match should award some points.
Game Over Screen
The game should end when the move counter reaches 0. Open up GUIManager.cs and add the following if
statement to MoveCounter
s’s setter, right below moveCounter = value;
:
if (moveCounter <= 0) {
moveCounter = 0;
GameOver();
}
This will work — mostly. Because GameOver()
is called right on the final move, combos won't count towards the final score. That would get you a one-star review for sure!
To prevent this, you need to create a coroutine that waits until BoardManager.cs has finished all of its shifting. Then you can call GameOver()
.
Add the following coroutine to GUIManager.cs below the GameOver()
method:
private IEnumerator WaitForShifting() {
yield return new WaitUntil(()=> !BoardManager.instance.IsShifting);
yield return new WaitForSeconds(.25f);
GameOver();
}
Now replace the following line in the MoveCounter
setter:
GameOver();
With:
StartCoroutine(WaitForShifting());
This will make sure all combos will get calculated before the game is over.
Now save all scripts, play the game and score those combos! :]
Where to Go From Here?
You can download the final project here.
Now you know how to make a basic Match 3 game by using Unity! I encourage you to keep working and building on this tutorial! Try adding the following on your own:
- Timed mode
- Different levels with various board sizes
- Bonus points for combos
- Add some particles to make cool effects
If you're enjoyed this tutorial want to learn more, you should definitely check out our book Unity Games by Tutorials, which teaches you to make 4 full games, scripting in C# and much more.
Here are a few resources to learn even more:
- Unity scripting tutorials: Video tutorials teaching you the basics of scripting in Unity.
- Introduction to Unity UI: Master Unity's UI system in this tutorial series.
- Introduction To Unity Particle Systems: Learn how particle systems work and spruce up your game with some sweet effects!
Can't wait to see what you all come up with! If you have any questions or comments you can post them in the Comments section.