- Home /
how to C# subclassing a subclass of MonoBehaviour?
I am having problems calling MonoBehaviour method such as OnCollisionEnter. start with
public class PowerUp : MonoBehaviour
{
void OnCollisionEnter (Collision collisionInfo)
{
Debug.Log("PowerUp");
}
}
this works fine but
public class Strong : PowerUp
{
void OnCollisionEnter (Collision collisionInfo)
{
Debug.Log("Strong");
}
}
when I put Strong on an object PowerUp's method get called not Strong's (hiding does not seem to work) I can't override OnCollisionEnter because it is not virtual in MonoBehaviour. I end up doing this which is ugly
public class PowerUp : MonoBehaviour
{
void OnCollisionEnter (Collision collisionInfo)
{
XOnCollisionEnter (collisionInfo);
}
virtual void XOnCollisionEnter (Collision collisionInfo)
{
Debug.Log("PowerUp");
}
}
public class Strong : PowerUp
{
override void XOnCollisionEnter (Collision collisionInfo)
{
Debug.Log("Strong");
}
}
does anyone have a more elegant solution?
Answer by XHawk87 · Dec 17, 2017 at 06:49 PM
For anyone searching this problem after 2017, you can now solve this much more elegantly by making the event methods 'protected' and 'virtual' in the superclass and 'override' them in the subclass. E.g.
public class PowerUp : MonoBehaviour
{
protected virtual void OnCollisionEnter (Collision collisionInfo)
{
Debug.Log("PowerUp");
}
}
public class Strong : PowerUp
{
override protected void OnCollisionEnter (Collision collisionInfo)
{
Debug.Log("Strong");
}
}
is overriding multiple times possible with protected methods?
What do you mean by multiple times? If you mean in another level of hierarchy, then yes. Any subclass can override any virtual method that any of the base classes has. Private virtual methods do not exist as they wouldn't make any sense. The visibility "protected" is visible only from inside the class any any subclasses as it's ment for class internal use only. In general it's not possible to change the visibility of inherited methods. That means a protected method always stays protected in C#
$$anonymous$$eep in $$anonymous$$d that when a method is declared virtual in a base class, not matter which reference type you use you will always call the top most overridden method. So overriding a method completely "replaces" the old method. However the new implementation of the method can use base.$$anonymous$$ethodName()
to call the method implementation from it's immediate base class but it doesn't have to.
Answer by testure · Jul 30, 2011 at 02:18 AM
Nope, that's how it's done. Just call the virtual method from the actual event (if you want the method to be private). Otherwise, just make it public or protected and you should be able to override the base method just fine.
Your answer
![](https://koobas.hobune.stream/wayback/20220612162413im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Trying to change a subclasses' variable 1 Answer
Renderer on object disabled after level reload 1 Answer
Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer