I am making a simple deck/card project. My project has a card prefab, with a Canvas child which would contain the UI version of the suit/card information. The information can be displayed with one card, but has problems when it comes to multiple cards. My theory on the matter is due to GameObject.FindGameObjectWithTag(string) finds the first instance of the gameObject that holds the tag. So, when I draw multiple cards, it will just rewrite the first card, rather than draw on the other cards.
I have tried Transform.Find(string), but it has turned up null, despite having a game object set in the Editor. One solution would be to use multiple tags with numbers after the name, ala topNum1, topNum2, etc., but doing that for 52 different numbers 3 times each sounds very repetitive, and frustrating. Is there a better way to do this?
Card code below:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
/// <summary>
/// This class sets up the card object, which can be used in a variety of games.
/// </summary>
public class Card: MonoBehaviour
{
int cardNum;//The Value of the card.
int cardType;//The ‘Suit’ of the card. Hearts, Spades, Diamonds, Clubs
//Text topText;
Text botText;
Text faceText;
Text topText;
//Creates a default card.
public Card()
{
cardNum = -1;
cardType = -1;
topText = GameObject.FindGameObjectWithTag("topText").GetComponent<Text>();
//topText = gameObject.transform.FindChild("Canvas").gameObject.transform.FindChild("TopCardValue").GetComponent<Text>();
botText = GameObject.FindGameObjectWithTag("botText").GetComponent<Text>();
faceText = GameObject.FindGameObjectWithTag("faceText").GetComponent<Text>();
}
//Creates a custom card, with the provided values.
public Card(int cN, int cT)
{
cardNum = cN;
cardType = cT;
topText = GameObject.FindGameObjectWithTag("topText").GetComponent<Text>();
//topText = gameObject.transform.FindChild("Canvas").gameObject.transform.FindChild("TopCardValue").GetComponent<Text>();
botText = GameObject.FindGameObjectWithTag("botText").GetComponent<Text>();
faceText = GameObject.FindGameObjectWithTag("faceText").GetComponent<Text>();
}
//returns the card’s value.
public int getCardNum()
{
return cardNum;
}
//returns the card’s suit.
public int getCardType()
{
return cardType;
}
//Sets the card’s value.
public void setCardNum(int newNum)
{
cardNum = newNum;
}
//Sets the card’s suit.
public void setCardType(int newType)
{
cardType = newType;
}
//Checks if the card’s value is a face card (Jack, Queen, King, or Ace)
public bool checkIfFace()
{
if (getCardNum() > 10 && getCardNum() < 15 || getCardNum() == 0)
return true;
else
return false;
}
//Checks if the card is a valid card.
public bool checkifValid()
{
if (getCardType() < -1 || getCardType() > 4)
{
Debug.LogError("Error: Card Type not valid. Card type is: " + getCardType());
return false;
}
if (getCardNum() < 0 || getCardNum() > 15)
{
Debug.LogError("Error: Card Value not valid. Card value is : " + getCardNum());
return false;
}
return true;
}
//Prints out the card information.
public void printOutCardInfo()
{
string value = "";
string suit = "";
if (getCardNum() == 1)
value = (" Ace");
else if (getCardNum() > 1 && getCardNum() < 11)
value = (getCardNum().ToString());
else if (getCardNum() == 11)
value = ("Jack");
else if (getCardNum() == 12)
value = ("Queen");
else if (getCardNum() == 13)
value = ("King");
else
Debug.LogError("Error: No Num Found! The number in question is: " + getCardNum());
switch(getCardType())
{
case 0:
suit = ("Hearts");
break;
case 1:
suit = ("Spades");
break;
case 2:
suit = ("Diamonds");
break;
case 3:
suit = ("Clubs");
break;
default:
Debug.LogError("Error: Suit not found.");
break;
}
topText.text = value;
botText.text = value;
faceText.text = suit;
}
}
Any and all help would be greatly appreciated. Thank you for your time.
EDIT: Since a few people asked for it, I have edited this question to include the code that this class is called in:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ListDeck : MonoBehaviour
{
//Card[] deckOfCards;
List<Card> deckOfCards;
// public GameObject spawner;
//public GameObject card;
//The default constructor.
public ListDeck()
{
deckOfCards = new List<Card>();
setUpDeck(4, 14);
randomizeDeck();
}
//Sets up the deck
public void setUpDeck(int numSuits, int numValues)
{
int counter = 0;
for (int i = 1; i < numValues; i++)//the thirteen values.
{
for (int j = 0; j < numSuits; j++)//The four suits.
{
Debug.Log("I value: " + i + " j value: " + j);
deckOfCards.Add(new Card(i, j));
counter++;//Increments the counter.
}
}
}
//Randomizes the deck so that the card dealout is random.
//http://answers.unity3d.com/questions/486626/how-can-i-shuffle-alist.html
public void randomizeDeck()
{
for (int i = 0; i < deckOfCards.Count; i++)
{
Debug.Log(i);
Card temp = deckOfCards[i];
int randomIndex = Random.Range(i, deckOfCards.Count);
deckOfCards[i] = deckOfCards[randomIndex];
deckOfCards[randomIndex] = temp;
}
}
//Prints out the deck for the game.
public void printOutDeck()
{
for (int i = 0; i < deckOfCards.Count; i++)
{
Debug.Log("Card " + i + ": ");
deckOfCards[i].printOutCardInfo();
}
}
public List<Card> getDeck()
{
return deckOfCards;
}
public void transferCards(List<Card> deckTo, int numCards)
{
for (int i = 0; i < numCards; i++)
{
deckTo.Add(deckOfCards[0]);
deckOfCards.RemoveAt(0);
}
}
}
Card
script attached to a GameObject? 2. How is it being called from another script? – Programmer