- Home /
Question about Overriding Functions
I have a base class then a subclass.
IF the base class has a OnTriggerEnter() function, how do I override that function with the subclass?
I thought, you have a base class, which you can declare it to be a virtual function, then down the line, you use override function.
THanks in advance.
Answer by Mike 3 · Nov 10, 2010 at 11:17 AM
In c# you do it like you say:
public class Base : MonoBehaviour { protected virtual void OnTriggerEnter(Collider other) {} }
public class Child : Base { protected override void OnTriggerEnter(Collider other) {} }
In javascript you'd just use function for both:
class Base extends MonoBehaviour { function OnTriggerEnter(other : Collider) {} }
class Child extends Base { function OnTriggerEnter(other : Collider) {} }
or in unity 2.6's javascript:
class Base extends MonoBehaviour { virtual function OnTriggerEnter(other : Collider) {} }
class Child extends Base { virtual function OnTriggerEnter(other : Collider) {} }
Only that you can't have non-public virtual functions. But if you declare your OnTriggerEnter function public it will work.
True, that was a thinko in my c# code. Protected is probably a better choice for the event functions though to stop you calling them manually
Your answer
![](https://koobas.hobune.stream/wayback/20220613082605im_/https://answers.unity.com/themes/thub/images/avi.jpg)