• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by MJacobsen186 · Sep 03, 2015 at 10:58 PM · c#unity 5instantiateprefablist

How to make TCG Deck (Instatiate based on Prefab)

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 w$$anonymous$$ch 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 t$$anonymous$$s, I have created a CardDB class w$$anonymous$$ch 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 w$$anonymous$$ch I can call the prefab that they reference and instatiate it when it is drawn. T$$anonymous$$s 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 wit$$anonymous$$n 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 t$$anonymous$$s! Thank you for looking and sorry about the wall of text.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MewEight · Sep 04, 2015 at 03:18 AM 1
Share
avatar image MJacobsen186 · Sep 07, 2015 at 02:07 AM 0
Share

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by IHackedDeath · Sep 07, 2015 at 04:03 AM

Hi MJacobsen186,

I had a quick skim over your code and problem, I thought I would add what I thought would help you out a bit.

With your cards, I would have them in a script list, then out of that list make a new list with all the cards that the user currently owns.

With all the cards that is in the list that the user owns, give them a menu that they can choose what cards are in their deck, and from that list you use that as their deck.

At the start of their turn, the list is randomized and then it instantiates prefabs of the id's from the cards on top like what MewEight suggested.

T$$anonymous$$s is my personal opinion anyway, you can use t$$anonymous$$s, or similar or even your own that you figure out :)

I hope t$$anonymous$$s helps.

Kind Regards,

IHackedDeath.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DarkLiquid43 · Nov 30, 2015 at 05:28 PM 0
Share

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Why is my Prefab Instantiating when the Scene is Loaded? 2 Answers

How do I Instantiate a prefab in a list? 0 Answers

Destroying assets is not permitted to avoid data loss. 0 Answers

How to associate underlying data structure to prefab at runtime? 1 Answer

Having multiple objects fire prefabs in different times C# 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges