- Home /
How to call an overriden method from parent class?
Hello, I have a basic question about inheritance. Let's say for example I have a father class named Character, and two children classes called Player and Enemy:
public class Character : MonoBehaviour {
// Use this for initialization
void Start () {
Hello ();
}
// Update is called once per frame
void Update () {
}
public virtual void Hello()
{
print ("Character");
}
}
I have this public virtual method I need to customize in the child classes:
public class Player : Character {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public override void Hello()
{
print ("Player");
}
}
And similarly for the Enemy class. I created 3 gameobjects and attached one of the 3 scripts to each one, and only the Character class gave me an output. The one above is just an example, what I would like to do is to call a series of instructions in the Character class (defined in the Character class), and add a custom behavior to some of them in the child classes. Is it the right approach to use hierarchy? Or should I use interfaces?
Answer by Landern · Aug 22, 2014 at 04:19 PM
base.Hello(); // refers to the inherited class hello method
this.Hello(); // refers to this instance and the hello method
Ok, but what I would like to do is to call the child Hello() method from the parent, I don't even know if it's possible honestly that's why I'm asking if it is or if I should resort to other options.
Suppose you could if you either registered the event or delegate in the constructor(or at some other point), otherwise the super class isn't aware through normal means. You could use interfaces or casting if your passing around the base class which is actually a child class.
See: http://stackoverflow.com/questions/6343301/up-casting-c-sharp
I wanted to write the AI for the characters in the parent so to write it only once, but it seems it's much more complicated than just rewriting some code in each of the children, so I think I'll go that way since I guess it's the more "natural" way to do this, thank you very much for the help ;)
Its not possible with normal architecture to call a child method from within a parent class. And if you think about it, it doesn't make much sense, because there is no guarantee that there actually is a child class, or there are not four chained child classes.
On the other hand if you do Character.Hello(); it calls the most overridden method. In this case the one on player.
Answer by shadoath · Sep 20, 2020 at 02:50 PM
I recently ran into this problem as well. Your approach to using the hierarchy is correct here, given their will be multiple enemies. Interfaces would best be used when you only want one object to exist that you can reference across the game.
One change I would make is to remove anything from the vitural
method and always override it.
I would actually add a warning to the virtual $$anonymous$$ethod, so that if it is not overridden that the Dev will be notified, instead of nothing happening at all.
Your answer
![](https://koobas.hobune.stream/wayback/20220613161245im_/https://answers.unity.com/themes/thub/images/avi.jpg)