- Home /
OnDrawGizmos for parent and child class
Hey all --
So I have a parent class which draws to the editor window on OnDrawGizmos(), which is great. However, I have a child of that parent class which wants to draw the same stuff as the parent on OnDrawGizmos(), plus a bit more info. So, I want to do something like
class Child : Base
{
void OnDrawGizmos()
{
//do stuff
base.OnDrawGizmos();
}
}
but this doesn't work! Visual Studio warns me that 'Parent.OnDrawGizmos() is inaccessible due to its protection level' and Unity gives me an internal compiler error, "System.InvalidOperationException: Method 'Child.OnDrawGizmos' does not have a method body" (which it clearly has). So... what's happening here?
Answer by Rod-Green · Dec 28, 2011 at 05:50 AM
parent class should be..
class ParentClass : MonoBehaviour
{
public virtual void OnDrawGizmos()
{
//parent gizmo rendering
}
}
the child should be
public class ChildClass : ParentClass
{
public override void OnDrawGizmos()
{
base.OnDrawGizmos();
//child gizmo rendering
}
}
Oh, durr -- I wasn't thinking about Unity finding functions via reflection and thought that OnDrawGizmos had to be a private non-virtual function since that seemed to be the default way it was declared. Thanks!