title: Unity 3D - Matching Pairs (2D) Game
body: <p>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.</p>
  
  <p>Click the yellow then click yellow again to make both disappear. (Until board is cleared or colours.)</p>
  
  <p>If clicked on yellow then anything other than yellow do nothing. </p>
  
  <p>Thanks in advance.</p>
  
  <p>Here is what the layout of the sprites looks like:</p>
  
  <p><a href="https://i.stack.imgur.com/ykxy3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ykxy3.png" alt="Game Screenshot"></a></p>
  
  <p>Would it be best to give every colour a tag?</p>
  
  <p>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. </p>
  
  <p>This is the code that I have implemented at the moment:</p>
  
  <pre class="lang-js prettyprint-override"><code>// [...]
  
      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);
      }
  }
  </code></pre>