Card games are amongst the oldest types of games and have been popular for centuries. In this article we will examine how to make a card game in Unity. Making card games is not only fun, but also a great way to build game development skills.

Discover how to make a card game in Unity

Today we will create a simple card-based Memory game using items from the Asset Store combined with custom written components.

PART 1 - Project and Asset Setup

1. Create a New Project.

Open Unity Hub and create a new 3D Project called “Memory”.

Create a New Project

2. Get Playing Cards Graphics Asset Package.

One of the amazing things about Unity is the Asset Store.  The Asset Store is a deep resource which can supply game developers with ready-to-use assets at low or no cost.  In our case, our card game will require textures and models with which to display playing cards.

  1. Go to the Asset Store in a web browser here.
  2. Search for “Free Playing Cards - Ultimate Sport Pack”.
  3. Click the “Add to My Assets” button.
Get Playing Cards Graphics Asset Package.

3. Import Play Cards Graphics Package to your project.

  1. Back in Unity, open the Package Manager from the “Windows” menu.
  2. Choose “Packages: My Assets” from the package selector.
  3. Download and Import the “Free Playing Cards - Ultimate Sport Pack” assets.
  4. Create a “Resources” folder inside of “Assets” and move “Free_Playing_Cards” within it.  “Resources” is recognized by Unity as a special folder which allows contained assets to be loaded directly from code using the Resources.Load() method.

4. Now that we have our Playing card assets, we can start laying out our game board.

  1. Right-click in the Hierarchy panel and create a Square Sprite.  Name it “GameBoard”
  2. Choose a Color for your GameBoard and Resize it to match the gray Viewport frame.
  3. In the Inspector Panel, choose “Add Component” -> “New Script” and name it “MemoryGame”.
Get Playing Cards Graphics Asset Package

5. Now it’s time to set up a Playing Card!

  1. From the Project Panel, in “Assets/Resources/Free_Playing_Cards” drag the card PlayingCards_2Club onto your GameBoard item in the Hierarchy View.  At first, you will not see it.  This is because you must set the rotation, scale, and position so that it is visible.
  2. We will make this card into a Prefab and reuse it for all of our game cards.
  3. Rename the card in the Hierarchy window to “PlayingCard”
  4. In the Inspector Panel, choose “Add Component” -> “New Script” and name the script “Card”.
  5. Drag the PlayingCard from the Hierarchy panel to the Assets folder to make it into a Prefab.
  6. When Unity asks you how to create the Prefab, choose “Original Prefab”
Now it’s time to set up a Playing Card!

6. Make “PlayingCard” instances.

Create as many “PlayingCard” instances on the GameBoard as you wish, in whatever layout you wish. Keep in mind that you should end with an even number so that all of the cards can be matched. Make sure the cards are children of the GameBoard.

Make “PlayingCard” instances.

7. Brighten the cards up.

The playing cards look pretty dark. Let’s create a GameObject -> Light -> Directional Light to brighten things up, and set its intensity to 2.

PART 2 - Scripting the Game

Open up the MemoryGame.cs script

Set up some variables that for general use.

Set up some variables that for general use

And now some variables for configuration and game state.

Set up some variables that for general use

Initialization time!

Initialization time!

Implement our Shuffle and GetRandomFromArray functions.

Implement our Shuffle and GetRandomFromArray functions

Finally, implement our game logic. Whenever a card is selected, store it to be compared.

Whenever a card is selected, store it to be compared.

When we have two cards selected, wait one second and then see if they are a match. If they are a match, remove them from the board. If not, flip them back over.

If they are a match, remove them from the board. If not, flip them back over.

We’re almost done! We just need to implement our Card functionality in Card.cs.

Establish state variables.

Establish state variables:

Set and check rank and suit.

Respond to user actions.

And finally, detect and respond to mouse clicks!

You should now have a fully functional card-matching game!

Now you know how to make a card game in Unity

Today we used freely-available Asset Store card assets with two custom-coded components to build a card-matching game. The same code can be modified to make a variety of card games… Blackjack, Poker, the possibilities are endless!

Up next, learn how to make a cool game in Unity.

To get a stronger and deeper understanding of the Unity development system, sign up for Create & Learn’s Unity development classes. There are two units of lessons to help you get comfortable and get running with Unity game development!

Written by Ken Patel, who started coding at age 10. Following his passion for video games, when he entered university he enrolled to study Computer Science. After graduation, he embarked on a career as a professional video game developer. Ken is excited to be sharing his passion, knowledge, and experience with the next generation of game makers through his role as a teacher here at Create & Learn.