Flipping Cards in Order

I’m trying to do a simple flipping card game where i need to get the key of “1” to flip the number 1 card. 2 for the number 2 Card, so and so on. my script for each cards goes something like this.

using UnityEngine;
using System.Collections;

public class Cards1Controller : MonoBehaviour {

	public int prefabID;
	public AudioSource audio;
	public int orderID;
	private int numPress = 0;



	// Use this for initialization
	void Start () {

	}
	
	// Update is called once per frame
	void Update ()
	{

		if(Input.GetKeyDown("1"))
		{
			numPress++;
			if ( numPress == prefabID)
			{
			audio = GetComponent<AudioSource>();
			audio.Play ();
			GetComponent<Animation>().Play("Flip_hideDuplicate");
			StartCoroutine("Remove");
			}

		}

	}	
	


	IEnumerator Remove()
	{
		yield return new WaitForSeconds (0.75f);
		Destroy (gameObject);
	}

For the variables orderID, i created it and manually named it 1,2,3,4,5… in the order of 1,1,5,5,6,6,5,4,4… thinking it might come in handy. For now i can get the cards to flip in order based on the first number 1 card then the next number 1 card, same for the other number cards. Is there any method i could flip the card in order of 1,1,5,5,6… , clicking other button that would not flip other cards and only flip when the right order is being clicked?
Thanks in advance.

So, If I understand you correctly, this is a solution:

I made two classes, one attached to every card and one attached to an empty game object (will handle input and progression) maybe this is not giving you exactly the result you want, but I do hope that it will point you to the right direction. First up, the card class: (There is absolutely no need at all to use different codes for each card. This coding practice will in time make you want to find the highest manmade building in the world and jump from it while screaming “AWMAGAD SO MANY SCRIPTS”. We want to avoid that by creating a simple class for the cards (Note that both the class scripts are summarized without all the commenting at the bottom of the answer):

public class Card : MonoBehaviour 
{
 //Here, only two fields are necessary. These are then set in the inspector.
 //The number of the card (which is the correct button to press) and the position
 //of the card (1 = first card, 2 = second card and so on...)
	public int cardNumber;
	public int cardPosition;
}

Now, this class can be assigned to all the cards on the board. And then in the inspector you can set the field values:

Then the class for the game object, handling input and progression:

public class Game : MonoBehaviour 
{
	//Now, in this game class we need to store references to our
	//cards, one way to achieve this is to create an array of the Card-class
	//we created as such:

	public Card[] cards;

	//We also need to know which position we are at now

	public int currentPosition;

	//Next up, the method for checking for the next card: I will call it
	//CheckForCard and I will pass an integer to it. That is what number
	//we think it have:

	public void CheckForCard(int number)
	{
		//Now we have both the numbers we need, we need to see if they're correct
		//We do this by iterating through our card array, using a foreach loop. This
		//will run once per member of the array:

		foreach(Card card in cards)
		{
			//The first to check is if the card the loop is currently checking is
			//at the correct position, if not, then we need to check the next card
			//in the array

			if(card.cardPosition == currentPosition)
			{
				//If the card position in this array is at the position we are now
				//checking for, we also need to check if the number is correct! That is when
				//we use our passed integer!
				if(card.cardNumber == number)
				{
					//CORRECT, now, move to the next position and stop the loop.
					currentPosition++;
					return;
				}
				else
				{
					//INCORRECT. Stop the loop and wait for a new input
					return;
				}
			}
		}
	}

	//In the update, we check for input:

	void Update()
	{
		if(Input.GetKeyDown(KeyCode.Alpha1))
		{
			CheckForCard(1);
		}
		if(Input.GetKeyDown(KeyCode.Alpha2))
		{
			CheckForCard(2);
		}
		if(Input.GetKeyDown(KeyCode.Alpha3))
		{
			CheckForCard(3);
		}
		if(Input.GetKeyDown(KeyCode.Alpha4))
		{
			CheckForCard(4);
		}
		if(Input.GetKeyDown(KeyCode.Alpha5))
		{
			CheckForCard(5);
		}
		if(Input.GetKeyDown(KeyCode.Alpha6))
		{
			CheckForCard(6);
		}
		if(Input.GetKeyDown(KeyCode.Alpha7))
		{
			CheckForCard(7);
		}
		if(Input.GetKeyDown(KeyCode.Alpha8))
		{
			CheckForCard(8);
		}
		if(Input.GetKeyDown(KeyCode.Alpha9))
		{
			CheckForCard(9);
		}
	}
}

Then assign this game script to an object and use the inspector to drag each card into the array:

Summary; both classes without disturbing comments:

public class Card : MonoBehaviour 
{
	public int cardNumber;
	public int cardPosition;
}

public class Game : MonoBehaviour 
{
	public Card[] cards;

	public int currentPosition;

	public void CheckForCard(int number)
	{
                foreach(Card card in cards)
		{
			if(card.cardPosition == currentPosition)
			{
				if(card.cardNumber == number)
				{
					currentPosition++;
					return;
				}
				else
				{
					return;
				}
			}
		}
	}

	void Update()
	{
		if(Input.GetKeyDown(KeyCode.Alpha1))
		{
			CheckForCard(1);
		}
		if(Input.GetKeyDown(KeyCode.Alpha2))
		{
			CheckForCard(2);
		}
		if(Input.GetKeyDown(KeyCode.Alpha3))
		{
			CheckForCard(3);
		}
		if(Input.GetKeyDown(KeyCode.Alpha4))
		{
			CheckForCard(4);
		}
		if(Input.GetKeyDown(KeyCode.Alpha5))
		{
			CheckForCard(5);
		}
		if(Input.GetKeyDown(KeyCode.Alpha6))
		{
			CheckForCard(6);
		}
		if(Input.GetKeyDown(KeyCode.Alpha7))
		{
			CheckForCard(7);
		}
		if(Input.GetKeyDown(KeyCode.Alpha8))
		{
			CheckForCard(8);
		}
		if(Input.GetKeyDown(KeyCode.Alpha9))
		{
			CheckForCard(9);
		}
	}
}

Maybe it’s a better idea to implement it in a state machine?

Mostly because that way you don’t have to use a timer, which is generally bad form.

I would states like these:

  1. standby (waiting for input)
  2. card number picked (input received looks for card to flip, and flips the ones in the right numbers)
  3. cleanup (remove elements and make sounds)
  4. end

state 3 loops back to state 1
but state 1 points to state 4 if there’s no more cards, or the game reaches an end condition.

If that’s too hard for you then there’s another option. You need a game controller object (which is generally always necessary).
And when you add the cards to the scene you add references to them in a Queue in the game controller. Then remove code from your cards that reacts in any way to user input (this is usually not a good idea anyways unless the cards are a player object)

In simpler terms:

  • When the game starts, the game controller runs and makes a queue(ordered list) of randomized cards (or specific order if that’s what you want).
  • When the queue is full, you render the contents of the queue (or render them as they are being generated, makes no difference at this point).
  • Then in the game controller’s update function you check for input, and compare input only to the top card in the queue structure, if it doesn’t match, nothing happens, if it does card does it’s thing and flips or goes away (whatever your game does).