C# Inheritance and Calling Functions

Haven’t used inheritance much before so I’m confused by the following C# scenario:

Parent object has method:

void OnTriggerEnter() {
MyFunction();
}

void MyFunction() {
DoSomething;
}

Child object has method:

voi MyFunction() {
DoAnotherThing;
}

But the child’s MyFunction never gets called! The parent’s MyFunction is called instead on trigger enter. I though a child’s functions were supposed to override the parent’s? Am I missing a qualifier of some kind, like “protected”, or “extends”?

virtual and override?

Could you elaborate please? Where should virtual be placed? What’s the reasoning behind it?

In your example it would most likely be:

void OnTriggerEnter() { 
MyFunction(); 
} 

virtual void MyFunction() { 
DoSomething; 
} 

Child object has method: 

override void MyFunction() { 
DoAnotherThing; 
}

You may also need to change the protection level (i.e. private, protected, or public) of the functions.

As for the reasoning, your best bet would probably be to Google/search for ‘c# polymorphism’ (you should be able to find some good references on the topic).

If I’m not mistaken the keyword virtual tells the compiler that this function may be overriten so keep an eye out!

Yup, thanks! Those two did the trick. Applied virtual to the function I’m overriding and set it to Protected too.

Sorry for the brevity, I had just returned home at 20 before midnight and was a little tired and wanted to point you in the right direction. Trust me, at that point it would have been a rambling mess of an answer, probably with a dirty joke thrown in there.