multiple selection in runtime

I am attempting to make a matching game, you know where you select two face down cards, they flip over and then if they match blablabla… i am having a really agrivating problem through and i have tried to fix it over and over and i keep running into a wall. with the way i have the code setup now, when i click on 1 box it sets both selection 1 and selection 2 to the first card, then when i click on the second card it sets both selection 1 and selection 2 to the second card. i attempted to fix this by nesting the second if statement in an else statement of the first if statement, but then it only ever calls the first if statement and not the nested if statement. i am at my wits end here and could really use some assistance.

using UnityEngine;
using System.Collections;

public class Flip : MonoBehaviour 
{
	public GameObject gameMaster;
	public GameObject _selection1;
	public GameObject _selection2;
	private Vector3 mousePos;
	public GameObject obj;
	public bool oneSelected;
	public bool twoSelected;

	// Use this for initialization
	void Start () 
	{
		obj = null;
		_selection1 = null;
		_selection2 = null;
		oneSelected = false;
		twoSelected = false;
	}
	
	// Update is called once per frame
	void Update () 
	{
		
	}

	void OnMouseOver () 
	{
		obj = GetComponent<Collider>().gameObject;
		//Debug.Log("Hovering over: " + obj.name + " " + obj.transform.position); 

		if (Input.GetMouseButtonDown (0))
		{
			Debug.Log("click");
			Debug.Log ("_selection1: " + _selection1 + " " + "_selection2: " + _selection2);
			if(oneSelected == false)
			{
				Debug.Log("_selection1 setter");
				_selection1 = obj;
				oneSelected = true;
				gameMaster.GetComponent<GameMaster> ().selection1 = _selection1;
				Debug.Log(_selection1 + "" + gameMaster.GetComponent<GameMaster> ().selection1);
			}
			if(oneSelected == true && twoSelected == false)
			{
				Debug.Log("_selection2 setter");
				_selection2 = obj;
				twoSelected = true;
				gameMaster.GetComponent<GameMaster> ().selection2 = _selection2;
				Debug.Log(_selection2 + "" + gameMaster.GetComponent<GameMaster> ().selection2);
			}

			Debug.Log ("_selection1: " + _selection1 + " " + "_selection2: " + _selection2);
		}
	}

	void reset()
	{
		//oneSelected = null;
		//twoSelected = null;
	}
}

and if for some reason this is effecting it, here is the gamemaster script.

using UnityEngine;
using System.Collections;

public class GameMaster : MonoBehaviour 
{
	public GameObject selection1;
	public GameObject selection2;

	// Use this for initialization
	void Start ()
	{
		Debug.Log ("Selection1: " + selection1 + "Selection2: " + selection2);
		selection1 = null;
		selection2 = null;
	}
	
	// Update is called once per frame
	void Update () 
	{
		//Debug.Log ("Selection1: " + selection1 + "Selection2: " + selection2);
	}
}

Currently your code is working like this


  1. oneSelected is false

  2. if condition on line 39 is true

2a) code sets oneSelected to true

2b) first card selected

  1. if condition on line 47 checks if oneSelected is true, which it is since 2a just set it to true

3a) code sets twoSelected to true

3b) second card selected


As you can see, your code immediately choses both cards. Use an if/elseif/else structure to avoid this and select one card at a time.

https://unity3d.com/learn/tutorials/modules/beginner/scripting/if-statements

I actually figured out what was wrong and facepalmed hard. I had this script on each card and so was calling each card separately as I hovered over it.