- Home /
Help with override function in inheritance,HELP with inheritance overide functions
So, I've been attempting to make a spell system using inheritance in unity. I have a Fireball scriptable object created, and residing in a public 'Spell' variable present in the playerscript. The playerscript reads this spell object (the fireball), and calls the cast function of it.
From what I understand, the 'virtual' and 'override' keywords present in my code should make it so that any Child class that I create of the Spell class, should be able to call the Spell Cast() function and have it override to their own specific spell effects.
Testing this out with the Debug.Log and Debug.LogError, shows that only the base.Cast() function is actually called, and never the overridden funtion in the Fireball Class.
This has had me stumped for a while, since I haven't even written any complex code here. Any help or advice would really be appreciated, thanks!
private void Update()
{
if (Targets.Count > 0){
if (Input.GetKeyDown(KeyCode.Mouse0)){
if (Targets.Contains(StaticGrid.selectedCell.gameObject))
{
Debug.Log("Before Cast");
Debug.Log(stateController.selectedWizard.Abilities[Index]);
stateController.selectedWizard.Abilities[Index].Cast();
Targets.Clear();
cam.camState = CameraScript.CamStates.Wizard;
}
}
}
}
(Both of the Debugs in the playerscript above run when expected, so I know the code reaches this point as I intend it to. The selectedWizard has a List populated with the abilities that he has at that time. In this case a Fireball at index 0, and a Magic missile at index 1. The second Debug confirms that it is reading the fireball correctly, but still using the function of its parent class)
public class Spell : ScriptableObject
{
//EVENTUALLY ADD ANIMATIONS TO SIT IN THE WINDOW OF THE CARD, SHOWING THE SPELL'S EFFECTS
[Tooltip("The name of the Spell")]
public new string name;
[Tooltip("A Detailed Mechanical Description of the Spell")]
public string description;;
public virtual void Cast() { Debug.LogError("Cast from default spell class"); }
}
[CreateAssetMenu(menuName = "Spells/Fireball")]
public class Fireball : Spell
{
public override void Cast()
{
Debug.Log("Cast Fireball");
}
}
[CreateAssetMenu(menuName = "Spells/MagicMissile")]
public class MagicMissile : Spell
{
public override void Cast()
{
Debug.Log("Cast Magic Missile");
}
}
This might be an issue with how you filled the stateController.selectedWizard.Abilities
list. Can you post the full script
Your answer

Follow this Question
Related Questions
Overriding an initial value in a subclass? 3 Answers
Question about Overriding 1 Answer
Override Function Scope 2 Answers
Overriding an inherited Enum? 1 Answer
An OS design issue: File types associated with their appropriate programs 1 Answer