2D Matching Game Question

Hi all I am currently working on a game 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.

65488-screen-shot-2016-03-08-at-181736.png

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);
    }
}

Thanks in advance.

On click:

  • If not have clicked anything yet, store the clicked object.
  • Else if color is of same kind (and adjacent), make both disappear/erase.
  • Else, store the clicked object.