Stop inherited class's function through return;

Hi, thanks in advance.

I have a base class with a function similar to

public virtual void Function()
    if (boolean)

I have a separate class with the function:

public override void Function()
Debug.Log("This shouldn't display?");

Since I have a couple of classes that inherit from the base class, I would like the baseclass to handle whether that code should run or not. The reason I can’t do an

if(boolean) then Function();

is because Function() get’s called by an InvokeRepeating and I’m trying to pause while the boolean is true.

Any help is appreciated!

How about this. In the base class call another function (MyFn) after the if statement and in child classes simply override MyFn instead of Function.

Something like this (you dont have to use abstract):

class abstract Base{
  public void Function() { 
    if (boolean) { return; } 
  protected abstract void MyFn();

You can’t do this, and it’s a good thing you can’t. Otherwise, you could look at your override method and not be sure whether the lines you see in front of you actually run or not. It would be a definitive cause for ridiculously hard to fix bugs.

If you want to halt a method’s execution, you have to do it in the method itself. So you have to add an if statement with a return clause in your overriding function. You can expose the condition you want to use in your base class.