We start off by creating our Spell class which will handle all the base attributes for a spell such as the spell name and others as seen below I also include a function called Cast which will be called when we want to cast the spell. We will want to make it a virtual void since we will be overriding it from classes that inherit the spell class. Another thing we need to include is a Spell function that is called when a spell is called / created

Make sure you put [System.Serializable] above the class Projectile and the class Spell otherwise they will not be visible in the editor.

public abstract class Spell

{

public string spellName;

public int manaCost;



public Spell(string spellname, int manacost)

{

spellName = spellname;

manaCost = manacost;

}

public virtual void Cast()

{



}

}





Next we need to create our projectile class that will override the Cast function in the Spell class of which projectile will inherit from. The reason for this is because projectile is a type of spell and can inherit everything inside the spell class and even add different features for example a game object that we will use for out projectile as seen below. I also add a variable for the reference to our spell manager when we create it afterwards just to save time

public class Projectile : Spell

{

public GameObject projectileOBJ;

[@HideInInspector] public SpellManager sm;

public Projectile(string spellname, int manacost)

: base(spellname, manacost)

{



} public override Cast()

{



}

}



Now we create our SpellManager class that will inherit MonoBehaviour. I’m not going to explain much about this class other than it is where we create an array of spells and a list for all the spells that you will see below. Note that we need to include System.Collections.Generic to access lists.

public class SpellManager

{

private List<Spell> spells = new List<Spell>();

public List<Spell> projectiles = new List<Spell();

public Spell[] equippedSpells = new Spell[6];

public int mana = 50;



void Awake()

{

for(int i = 0; i < projectiles.Count; i++)

{

projectiles[i].sm = this;

// Add the projectiles to the spell list

spells.Add(projectiles[i]);

}

}

}



We then attach this SpellManager script onto a new empty game object which we re-name to SpellManager. So now we have a visible list and a visible array. If you want to make a projectile simply add one to the list values and change the attributes to fit as needed. Next we need to add the cast function in spell manager to fit the spell needed. (so projectile for example). We also have to set up a function that calls the Cast function.

public class SpellManager

{

private List<Spell> spells = new List<Spell>();

public List<Spell> projectiles = new List<Spell();

public Spell[] equippedSpells = new Spell[6];

public int mana = 50;

void Awake()

{

for(int i = 0; i < projectiles.Count; i++)

{

// Add the projectiles to the spell list

spells.Add(projectiles[i]);

}



for(int i = 0; i < equippedSpells.Length; i++)

{

equippedSpells[i] = spells[Random.Range(0, spells.Count)];

}

}



public void CastProjectile(Projectile projectile)

{

GameObject GO = Instantiate(projectile.projectileOBJ,

transform.position, transform.rotation) GO.name = projectile.spellName;

// You can then add force or what ever to GO }



public void CastCaller(int i)

{

equippedSpells[i].Cast();

}

}

So now we have the Casting set up, we need to call the sort out the Cast function inside of the projectile.

public class Projectile : Spell

{

public GameObject projectileOBJ;

[@HideInInspector] public SpellManager sm;

public Projectile(string spellname, int manacost)

: base(spellname, manacost)

{



} public override Cast()

{

if(sm.mana >= manaCost)

{

sm.mana -= manaCost;

sm.CastProjectile(this);

}

}

}





And that’s basically it. All you have to do now is assign the buttons pressed for each equipped spell and call the CastCaller function with the parameter of the equipped spell index.