Search code examples
unity-game-engine2dunityscript

Unity 3D - Matching Pairs (2D) Game


I am currently working on a game in Unity3D where you must click on colour pairs to match them and then they disappear. I am using 2D sprites to do this but I am struggling in terms of the logic to erase the pair when both is clicked via mouse.

Click the yellow then click yellow again to make both disappear. (Until board is cleared or colours.)

If clicked on yellow then anything other than yellow do nothing.

Thanks in advance.

Here is what the layout of the sprites looks like:

Game Screenshot

Would it be best to give every colour a tag?

Here is what I want to happen: When the game starts it picks 3 colours from an array of 6 then randomly places them (2 of each colour) on the screen. You then have to click the colour for example green (it will highlight) then click on the other green, and they will both disappear. If you were to, say, click on the green first then yellow, the game will just end.

This is the code that I have implemented at the moment:

// [...]

    if (Input.GetMouseButtonDown(0))
    {
        CastRay();
    }       
}

function CastRay() {
    var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);

    var hit: RaycastHit2D = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);

    if(hit.collider != null)
    {
        // Number is the amount of objects on the screen at one time.(6)
        number --;

        //Test to see if a mouse click interacts with the 2D Sprite.(Then destroys it)
        Debug.Log ("Target Position: " + hit.collider.gameObject.transform.position + gameObject.tag);

        Destroy(hit.collider.gameObject);

    }


    // This when the number hits 0 the level restarts (To check random elements) 

    if (number == 0)
    {
        Application.LoadLevel (0);
    }
}

Solution

  • You need to have two variables storing the type of clicked cards and boolean variable to store info if you are clicking first or second card (false for . And then on click event you need to check few things: 1. Need to check if you are clicking on first or second card. If it is first card, check the boolean variable if it is false. If yes: change it to true and show the card. Store the card type in the firsClicked variable. 2. If it is second click your check for boolean should be true. In that case you should check if the type of second card is same as first card. if true - Profit. If false, turn the cards back. Thats all the logic here.

    EDIT Ok, here it is step by step.

    1. You need to have a GameObject variable to store the circle that is chosen in the first click. Lets say private GameObject firstCircle = null; . Put it outside your click method so it is not initialized on every click.
    2. Every of your circle objects have to have some fields that store their colour. I do not know how do you set them, I guess that there is a tag? I guess that they have tags like "green", "red" and so on?
    3. In your click event you have to have if-else. Something like that (pseudocode only):

      if(firstCircle == null)
      {
      firstCircle = hit.collider.gameobject; // this will store the first clicked circle for later comparison
      }else{
      firstCircle = null;
      if(firstCircle.tag == hit.collider.gameObject.tag)
      {
      //here you can destroy both objects or add points or whatever like
      Destroy(firstCircle); Destroy(hit.collider.gameObject);
      }else{
      // here you do what you want when circles are not the same } }

    Plese not this is just pseudocode and I could not test it, but I hope you get the idea behind this. Generally you need to store the first circle after the first clik to compare it with the circle after the second click. Please have in mind that you have to check if user do not click the same circle twice (I did not include this here)