First part of tutorials on how to create Memory game.

You can play the game in the browser right here:

Video showing the game:

To begin with with create new Unity 2D project.

When Unity has finished creating new project, make new folders “Scenes”, “Scripts”, “Sprites” and “Prefabs”. To do that right click in the Assets window, then click Create→Folder. After you have finished it should look like this.

Folders

Save current scene to “Scenes” folder, name it “MainScene”, this could be done by:

  • choosing option from FileSave Scene,
  • Ctrl/Cmd + S

After that put following sprites into “Sprites” folder.

 

Use the import settings shown below for all card sprites (to select all sprites: click on first card and then while holding shift on the last one).

This settings are best for pixel art sprtes, because it will show the sprites how they should look and not try to optimise them for bigger resolutions. Take sprite named Memory-0 and put it in Hierarchy and reset its position – click little gear in transform placed next to book with question mark and choose Reset. After that add component BoxCollider2D to it, which can be found in Add Component→Physics2D→BoxCollider2D. Mark Is Trigger field, because it is necessery for OnMouseDown function that one of the scripts will be using. Also change name of this GameObject to “Memory1” – click on Memory_0, delete text in this box,  type “Memory1” and hit enter.

Now add “CardController” script to Memory_0 GameObject(Add Component→New Script). After that edit script by clicking twice on it in Assets window or by clicking little gear over that script and choosing Edit Script option. “CardController” script should look like this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// © 2017 TheFlyingKeyboard
// theflyingkeyboard.net

public class CardController : MonoBehaviour
{
    [SerializeField]
    private string _cardName; //It will be used to check if two cards match

    private bool _isUpsideDown = false; //To know if card was flipped

    private Sprite _backSideCardSprite; //Sprite that represents back of the card

    [SerializeField]
    private Sprite _frontSideCardSprite; //Sprite that represents front of the card

    private SpriteRenderer _spriteRenderer;

    private void Start()
    {
        _spriteRenderer = GetComponent<SpriteRenderer>();

        _backSideCardSprite = _spriteRenderer.sprite;
    }

    public string cardName
    {
        get
        {
            return _cardName;
        }
        set
        {
            _cardName = value;
        }
    }

    private void OnMouseDown() //This function is called each time player clicks on GameObject
    {
        if (!_isUpsideDown)
        {
            ChangeSide();
        }
    }

    void ChangeSide()
    {
        if (!_isUpsideDown)
        {
            _spriteRenderer.sprite = _frontSideCardSprite;
            _isUpsideDown = true;
        }
        else
        {
            _spriteRenderer.sprite = _backSideCardSprite;
            _isUpsideDown = false;
        }
    }
}

After the script was created put it to “Scripts” folder in Unity. Next step is to fill “CardController” “Card Name” field with “Memory1” and field “Front Side Card Sprite” with sprite “Memory_1”. To change first field simply write “Memory1” in it, to add sprite to the second field click on small circle icon and choose “Memory_1” sprite from Select Sprite window or go to “Sprites” folder and draw needed sprite to it.


“Memory1” GameObject should look like this:

Now click on “Memory1” card in Hierarchy and drag it into “Prefabs” folder. Click again on that GameObject in Hierarchy and duplicate it, it could be done by

  • Ctrl/Cmd + D
  • right clicking on it and choosing Duplicate option

Change name of duplicate to “Memory2” and change fields “Card Name” and “Front Side Card Sprite” of “CardController” script to “Memory2” and “Memory_2” sprite as shown on image below:As for previous card drag this card to “Prefabs” folder to save it for later use.

Now repeat duplicating cards to use all card sprites. (Repeat previous step, but simply change sprites and “Card Name” field to “Memory3”, “Memory4”, …).

There should be 9 Prefabs in “Prefabs” folder:

Next step is creating a script to manage interactions between player and cards.

Next Part Of Tutorial: GameManager
Memory Game Tutorial Part 1 – Cards
Tagged on:             

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.