- Home /
Adding inherited class to list
Hi I have 4 classes that work together as follows
BSpell (contains all functions for generic spells
SpellProperties : BSpell (each spell will have its own such as "fireballProperties, this contains all the detail information such as damage ect)
SpellMechanics : SpellProperties (contains mechanics of spell, used on a gameobject prefab)
PlayerClass
This is where my problem comes in, in PlayerClass i have a public List spellBook. i would like to add the different SpellProperties to this list (through code), but if BSpell inherits from mono then a new instance cannot be instantiated. if BSpell inherits from ScriptableObject then SpellMechanics cannot affect its gameobject.
Any help would be appreciated
Answer by mattmanj17 · Oct 21, 2013 at 07:33 PM
Your notation is a little vague. You say each spell "has a" spellProperties object which keeps track of information such as damage, etc, but the notation "SpellProperties:BSpell" indicates spellProperties inherits from BSpell.(if this distinction dose not make sense, look into "has a" vs "is a")
I will answer your question for each case:
Lets say your character has three scripts attached to him, say FireBall, MagicMissil, and ConeOfCold.
If each of these spells "is a" SpellProperties (if they inherit from it), then just use
spellbook.add(gameobject.getcomponent<fireball>());
If each of these spells "has a" SpellProperties (if each one has a spell properties as an element to keep track of various stats), then use this
spellbook.add(gameobject.getcomponent<fireball>().fireballProperties);
For each other "spell" you would just use its name instead of "fireball".
if you want to add a new spell to the player, just use addcomponent().
Tnx for the answer, my Bspell has all the fields but for instance I make FireballProperties that inherits from BSpell and all fireball details are then assigned such as name, dmg etc, FireballProperties is not added onto a game object, I would like to add the FireballProperties to the list. But I cannot instatiate "new" because of monobehaviour. Fireball$$anonymous$$echanics inherits from FireballProperties and is attached to a prefab and contains the spell's movement and so on.
What I'm doing now is splitting properties and mechanics so that mechanics is monobehaviour and properties is scriptableobject and then just referencing properties from mechanics
glad you fixed your problem. if you figured it out, could you close the question or mark the answer as correct, so the question dosn't stay up.