Problem setting up instantiated card id


I have a silly problem - I’m trying to create a simple deck of cards. I have a GameObject deck which has a script attached:

public List<string> cards;
	public GameObject emptyCard;
	public string activeCard;

	// Use this for initialization
	void Start () {

		cards = new List<string>();


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

		var randomNumber = Random.Range(0,cards.Count);

		print ("random number: "+randomNumber);

		var drawnCard = cards[randomNumber];

		activeCard = drawnCard;

		print("drawn card: "+activeCard);

		Instantiate(emptyCard,new Vector3(0f,0f,0f),Quaternion.identity);

		var spawnedCard = emptyCard.GetComponent<activeCard>();

		spawnedCard.cardId = activeCard;

		print ("card id set: "+spawnedCard.cardId);

		RemoveFromDeck ();


	void OnMouseDown (){


	void RemoveFromDeck (){

		print ("deleted: "+activeCard);


Then Ihave a prefab emptyCard, with script:

public class activeCard : MonoBehaviour {

	public string cardId;


When I run scene, after i click on deck I get a weird results: variable cardId of instantiated card is different then variable activeCard set in deck’s script. Do you have any ideas of what I’m doing wrong? Much thanks for help!

  1. You have a public variable emptyCard (that has a prefab attached?)
  2. You then instantiate a clone of this prefab but don’t keep a reference to the clone.
  3. You then set the cardID of the prefab

Try swaping in these line:

   GameObject clonedCard = Instantiate(emptyCard,new Vector3(0f,0f,0f),Quaternion.identity) as GameObject;
   var spawnedCard = clonedCard.GetComponent<activeCard>();