Hi guys, I am still pretty new at coding and am working on a TCG in unity.
In my game, I am working on making the deck which can store 3 different types of cards, Magic, Monster and Modifier. I have scripts for a monster Card, Magic Card and Modifier Card which all inherit from the class Card.
In my project, the deck just stores a list of gameobjects and I have a reference to a prefab that has a simple card script attached to it. I have a cardAsset class which has premade data for all the different cards I want my game to have.
Based on what cards are in the deck, I am trying to create gameobjects during loading for each card in the deck and add the respective scripts to the gameobject based on what card I am instantiating.
The problem is that I want to create the gameObject using only 1 method without having to override the data to fit the 3 different types of cards. This way, I am hoping to later possibly add card effects through a separate script.
I have already tried making the create method take in a constructor of card type. The game runs if I do this but, I can only access the card script related data of the card if I do this.
Any suggestions on what I should do??
To help understand better, my code inherits as follows.
|- MonsterCard
Card – MagicCard
|- ModifierCard
Below is my code for the create method in my deck.
GameObject CreateCard(Card cardDate)
{
GameObject newCard = Instantiate(CardPrefab);
Card newCardData = newCard.GetComponent<Card>();
newCard.tag = "Card";
newCard.name = cardDate.cardName;
newCardData.cardName = cardDate.cardName;
newCardData.manaCost = cardDate.manaCost;
newCardData.isFaceUp = false;
newCardData.cardtype = cardDate.cardtype;
newCard.transform.position = theDeckPosition;
newCard.transform.rotation = theDeckRotation;
return newCard;
}