Spell Library

I want to create a game with spells. So far I’ve been making a spell system with superclass SpellClass (which derives from MonoBehaviour), sub-classes Offense_Spell and Healing_Spell and then subclasses of specific spells of that. Atleast, that’s the idea.
Then I create a class called SpellLibrary with Dictionary<string, SpellClass> to keep a list of all spells.

However, I can’t get this to work. So I’m guessing I’m not doing it right. Is there someone that could give me a general idea of how to set up the classes for spells and the library, maybe with examples of code or other projects? I would be very grateful for some pointers.

Components cannot be instantiated with the new keyword.

First, you should ask yourself if your spell class really need to inherit from monobehaviour. Does it have to be attached to a gameObject ?

If it does, here is a correct function for insantiating your class (UnityScript users, correct me if I’m wrong please) :

// If someone can change this to generic for the subclasses, I'd appreciate it.
public static function InstantiateSpell() : SpellClass
    var obj : GameObject = GameObject( "NewSpell" );
    return obj.AddComponent( typeof(SpellClass) );

Then, when you add a spell :

// InstantiateSpell.<Spell_RockFist>() ?
AddSpell("Rock Fist", SpellClass.InstantiateSpell() );

Looks like Berenger’s solution will instantiate a copy of SpellClass every time you cast the spell. I think you’re intending to have a library of SpellClass templates which get Updated for cooldowns - and the SpellClass creates the spell instance already…right?

It’s probably simplest to to attach your SpellLibrary to a GameObject (eg. the player) and have the SpellLibrary.Update manually call Update on each of the SpellClasses in your library. That way they don’t need to derive from Monobehaviour at all.