- Home /
CCG: Creating a card object
Hello,
I'm fairly new to Unity and currently have a few problems figuring out how to efficiently combine scripts with objects contained in my world. Coming from writing in pure C#, I'm used to treating every class as an object yet this approach sometimes seems to not be working properly.
For example, I am in the process of creating a CCG and have a few problems actually creating the cards. So far I created a prefab which is to be used for cards. This prefab consists of two planes representing front and back of the card, a rigidbody, and a box collider so I can have physically somewhat acurate cards. I want to load cards from a preexisting cardlist at the beginning of a game, so I dont have to create every card within the Unity Editor. Therefore the properties of cards need to be able to be modified at runtime.
Were I in pure C#, I would have a class "Card" containing a variable for every property I'll ever need which would simply be filled as I create the cards for a deck.
My question is how do I go about creating a card object within Unity. Do I just create a single prefab for a card, attach a card script containing potential properties to it, and then just instantiate multiple prefabs when I load the deck? Or how do I attach a physical representation, like the prefab, to an object created by a script?
Sorry for the wall of text, I just hope I made my problem clear enough. This is probably a pretty stupid question, help would still be much appreciated.
Thanks in advance
Answer by Berenger · Mar 08, 2012 at 12:15 AM
I'm not totally sure to understand what you mean but, I'd say you need a prefab with a script Card.cs in which you implement the class Card inheriting from MonoBehaviour. Then make it a prefab. Second step is to have a way to setup all the deck. I see three ways :
Duplicate the instanciation of that prefab as many time as you need (in the hierarchy, not the asset folder!) and set it up manually.
Create a class CardBuilder (or whatever), which doesn't inherit from anything and with all the var of Card, plus a reference to the prefab. Add [System.Serializable] on top of it so you can see it in the inspector later. Once this is done, Create a MonoBehaviour inherited script with an array of CardBuilder, drag it on an object in the scene and set it up. At runtime, go through that array, Instantiate the prefab and copy the values.
If there is a way to build them automatically, you don't need the class above. I guess you have to iterate 13 times * 4 etc.
Answer by Nargrimm · Mar 08, 2012 at 08:35 AM
Yes, this is pretty much exacly what I needed. I've now set it up in a a way, that at the start of the game, the prefab is cloned 50 times for each deck in play and then assigned it's values (right now this is done statically). Shouldn't be too much of a problem assigning these values from a seperate cardlist later down the line. Then every instance created this way is added to a list, holding every card currently in the deck. Thus far this seems to work like a charm.
Thanks a lot for your answer.