Hello everyone! I have been trying to make the skeleton of a card game (TCG/CCG). I am having a bit of trouble wrapping my head around the deck element. So far I have a Card class which is serialized to construct all the elements of cards in the game.
Card class:
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Card {
public string cardName;
public int cardID;
public int cardMana;
public int cardAttack;
public int cardDefense;
public int cardSpeed;
public CardType cardType;
public SubType cardSubType;
public Faction cardFaction;
public int cardGems;
public GameObject cardPrefab;
public enum CardType{
Summon,
Chant,
Equipment
}
public enum SubType{
Creature,
Structure,
Armor,
Weapon
}
public enum Faction
{
Neutral,
Chaos,
Water,
Earth,
Wind,
}
public Card(string name, int id, int mana, int attack, int defense, int speed, CardType type, SubType subType,
Faction faction, int gems)
{
cardName = name;
cardID = id;
cardMana = mana;
cardAttack = attack;
cardDefense = defense;
cardSpeed = speed;
cardType = type;
cardSubType = subType;
cardFaction = faction;
cardGems = gems;
cardPrefab = Resources.Load<GameObject>("Prefabs/" + name);
}
}
In addition to this, I have created a CardDB class which manages all of the cards in the game. I will reproduce its code here:
Card database:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CardDB : MonoBehaviour {
public List<Card> cards = new List<Card>();
void Start(){
cards.Add (new Card ("Annoyed Knight", 0, 1, 2, 2, 3, Card.CardType.Summon,
Card.SubType.Creature, Card.Faction.Neutral, 0));
cards.Add (new Card ("Infuriated Bear", 1, 3, 5, 4, 4, Card.CardType.Summon,
Card.SubType.Creature, Card.Faction.Neutral, 0));
cards.Add (new Card ("Ballistic Bear", 2, 4, 5, 6, 5, Card.CardType.Summon,
Card.SubType.Creature, Card.Faction.Neutral, 0));
}
}
I’m wondering how to add these items to list in which I can call the prefab that they reference and instatiate it when it is drawn. This is what I currently have for the deck script:
Deck:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.EventSystems;
public class PlayerSummonDeck : MonoBehaviour {
public List<Card> playerSummonDeck = new List<Card>();
public CardDB database;
void Start ()
{
database = GameObject.FindGameObjectWithTag ("CardDB").GetComponent<CardDB>();
playerSummonDeck.Add (database.cards [0]);
}
}
Overall question: How do I make it so that when I click on the deck it draws the top card of the list, instatiates it as the prefab? Is there a way to reference the prefab attached to the card within the database?
I have created a prefab for each of the 3 cards referenced in the database. The script above sets it to load the prefab from the resources folder called Prefabs + name where “name” is the name of the card in the database. I would really appreciate some help with this! Thank you for looking and sorry about the wall of text.