Card Game [C#]

Hi everyone…I’m new to all of this, especially Unity3D. I started to design a pack of cards i could use for a game idea i have, Basically i want to have a card show up randomly when you tap a button on the screen (Android / IoS). Essentially giving you a shuffled pack feel, But… i want the cards called to be removed from the Random List of cards that Haven’t been called, that way the same card won’t show in the same game, until Reset.

Could anyone point me in the right direction with this?

I’ve made all the cards using Photoshop and imported them onto a Plane (Of which i made a prefab). How would i assing a specific number (array?) to each of these Card Prefabs…allowing me to call them to position randomly using a button?

Thanks in advance.

I have edited the answer to include all considerations in the comments. First though, make sure your script name is the same as the class name (e.g. public class DeckOfCards : MonoBehaviour → script name should be DeckOfCards) :

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class DeckOfCards : MonoBehaviour
{
    public List<GameObject> deck = new List<GameObject>();

    private List<GameObject> cards = new List<GameObject>();
    private List<GameObject> hand = new List<GameObject>();
	private int cardsDealt = 0;
	private bool showReset = false;

    void ResetDeck()
    {
		cardsDealt = 0;
		for (int i = 0; i < hand.Count; i++) {
			Destroy(hand*);*
  •  }*
    

hand.Clear();
cards.Clear();
cards.AddRange(deck);

  •  showReset = false;*
    

}

GameObject DealCard()
{
if(cards.Count == 0)
{

  •  	showReset = true;*
    
  •  	return null;*
    

//Alternatively to auto reset the deck:
//ResetDeck();
}

int card = Random.Range(0, cards.Count - 1);
GameObject go = GameObject.Instantiate(cards[card]) as GameObject;
cards.RemoveAt(card);

  •  if(cards.Count == 0) {*
    
  •  	showReset = true;*
    
  •  }*
    

return go;
}

void Start()
{
ResetDeck();
}

void GameOver()
{

  •  cardsDealt = 0;*
    
  •  for (int v = 0; v < hand.Count; v++) {*
    
  •  	Destroy(hand[v]);*
    
  •  }*
    

hand.Clear();
cards.Clear();
cards.AddRange(deck);
}

void OnGUI()
{

  •  if (!showReset) {*
    
  •  	// Deal button*
    
  •      if (GUI.Button(new Rect(10, 10, 100, 20), "Deal"))*
    
  •  	{*
    
  •  		MoveDealtCard();*
    
  •  	}*
    
  •  }*
    
  •  else {*
    
  •  	// Reset button*
    
  •      if (GUI.Button(new Rect(10, 10, 100, 20), "Reset"))*
    
  •  	{*
    
  •  		ResetDeck();*
    
  •  	}*
    
  •  }*
    
  •  // GameOver button*
    

if (GUI.Button(new Rect(Screen.width - 110, 10, 100, 20), “GameOver”))

  •  {*
    
  •  	GameOver();*
    
  •  }*
    

}

void MoveDealtCard()
{
GameObject newCard = DealCard();

  •  // check card is null or not*
    
  •  if (newCard == null) {*
    
  •  	Debug.Log("Out of Cards");*
    
  •  	showReset = true;*
    
  •  	return;*
    
  •  }*
    
  •  //newCard.transform.position = Vector3.zero;*
    
  •  newCard.transform.position = new Vector3((float)cardsDealt / 4, (float)cardsDealt / -4, (float)cardsDealt / -4); // place card 1/4 up on all axis from last*
    
  •  hand.Add(newCard); // add card to hand*
    
  •  cardsDealt ++;*
    

}
}
Now here’s where you drag and drop your card prefabs. Look in the inspector and you’ll see Deck : Size : 0 .
[943-inspect+1.png|943]*
Now change the 0 to 52. you should have the second screenshot.
[944-inspect+2.png|944]*
This is where you drag and drop into the list. So ignore my last comment, you really should do it this way , and not the long-hand way =]
@whydoidoit this is from your JS answer, I hope you don’t mind. I just needed a place to upload screenshots. I saw your link on another qn (orbit cam), I think it’s really nice that you are sharing scripts. Thanks , I shall be browsing your 'site later. All the best =]
*
*

You could create a script with a list of prefabs, use the inspector to make it 52 long and then drop the prefabs into each slot. That gives you 52 cards that you can access.

Your then need to make a copy of the deck and deal out the cards one at a time in random order, removing it from the copied deck.

This script might help:

#pragma strict

import System.Collections.Generic;

var deck : List.<GameObject>;

private var cards : List.<GameObject> = new List.<GameObject>();

function ResetDeck()
{
	cards.Clear();
	cards.AddRange(deck);
}

function DealCard() : GameObject
{
	if(cards.Count==0)
    {
		return null;
        //Alternatively to auto reset the deck:
        //ResetDeck();
    }
	
	var card : int = Random.Range(0, cards.Count-1);
	var go : GameObject = GameObject.Instantiate(cards[card]) as GameObject;
	cards.RemoveAt(card);
	return go;
}

function Start()
{
	ResetDeck();
}

function RemainingCardCount() 
{
    return cards.Count;
}

When you call DealCard it will create a new GameObject from the prefab - you’d then need to position it etc. After 52 cards it would start returning “null” or you could check the RemainingCardCount(). In the script you could delete the return null; line and uncomment the ResetDeck() line to have it automatically start again.

You would probably want to call this in an OnGUI function that would show your button and then position the created game object according to the camera you have and where you want the item to appear. A simple OnGUI might look like this:

function OnGUI()
{
    if(GUILayout.Button("Deal"))
    {
        var newCard : GameObject = DealCard();
        newCard.transform.position = Vector3.zero; //Replace with your location
    }
}