Search Unity

Inheriting functions like Start or Update

Discussion in 'Wish List' started by Factoid, Jun 1, 2009.

  1. Factoid

    Factoid

    Joined:
    Mar 30, 2008
    Posts:
    69
    EDIT: Sorry, this should have been posted to Scripting, please move it.

    Wanted to know if there was a best practice for chaining functions like Start or Update. It seems to me like they should be virtual. The following situation produces a warning.

    Code (csharp):
    1.  
    2. class A : MonoBehaviour
    3. {
    4.   void Start()
    5.   {
    6.      //DoSomething
    7.   }
    8. }
    9.  
    10. class B : A
    11. {
    12.   void Start()
    13.   {
    14.     base.Start();
    15.     //DoSomethingElse
    16.   }
    17. }
    18.  
    Specifying B::Start as an override function doesn't work because it's not virtual. I suppose the only valid option to remove the warning is to specify it as a new function. That doesn't prevent base.Start() from doing its job, but is there a Unity sanctioned way to deal with inheritance w.r.t. Unity event hooks?

    Thanks,
    Factoid
     
  2. MatthewW

    MatthewW

    Joined:
    Nov 30, 2006
    Posts:
    1,356
    We usually just do our own functions -- OnStart or OnAwake -- but in general we're able to avoid it altogether. You're usually better off with a component-based design versus class hierarchies.
     
    davidrochin likes this.
  3. Factoid

    Factoid

    Joined:
    Mar 30, 2008
    Posts:
    69
    Usually I agree. We make heavy use of component architecture, but in this instance we have components that are looking for "Weapons" to act upon, and we have some weapons which share base code, but implement some actions differently. Our previous component solution degenerated into a mess of SendMessage calls and case specific logic.

    We use components everywhere we have a 'has a' relationship, but when it's an 'is a' relationship, anything other than inheritance is a PITA.
     
  4. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    You can just make your base class method virtual and use override on the derived class method and everything will work work as expected. It works correctly with Unity events such as Start as well as with SendMessage and the like.
     
  5. Factoid

    Factoid

    Joined:
    Mar 30, 2008
    Posts:
    69
    So even though MonoBehaviour::Start() isn't virtual, I can redefine it as virtual in a derived class? Thanks for the tip, I'll give that a whirl.
     
  6. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    In fact, MonoBehaviour doesn't define Start or any of the other event methods at all. They're not part of its interface. Instead, when you implement Start in your class, you're actually providing the base class implementation and you're free to make it virtual if you want to. This works because Unity uses reflection to look up these special methods by name and signature instead of by calling them through a predefined interface.
     
    fhs15, CrandellWS and irenya like this.
  7. Factoid

    Factoid

    Joined:
    Mar 30, 2008
    Posts:
    69
    That would explain why they suggest not implementing those functions if they don't do anything. Thanks again for the clarification.
     
    fhs15 likes this.