Search Unity

Inheritance and Update()/Start()/Awake()

Discussion in 'Scripting' started by Antoids, Apr 6, 2018.

  1. Antoids

    Antoids

    Joined:
    Apr 11, 2015
    Posts:
    12
    When experimenting with script inheritance I noticed the following behavior:

    Code (CSharp):
    1. public class Base : Monobehaviour {
    2.  
    3.     void Start(){
    4.     Debug.Log("0");
    5.     }
    6. }
    7.  
    8. public class Inherit : Base {
    9.  
    10.     void Start(){
    11.     Debug.Log("1");
    12.     }
    13. }
    14.  
    15. public class LastInherit : Inherit {
    16.  
    17.     void Start(){
    18.     Debug.Log("2");
    19.     }
    20. }
    If these three are separate scripts, and you attach LastInherit to a GameObject, the only output will be "2". This kind of makes sense since that's the script running, but I would also think that due to inheritance, all three should be included with LastInherit. Is there a way to not make LastInherit's Start() supercede all others? Is there a way to yield "0", "1", and "2" as the output without attaching all three scripts to the same GameObject? I would think attaching all three scripts would somewhat defeat the purpose of inheritance.
     
    Last edited: Apr 6, 2018
    Rs likes this.
  2. Pagefile

    Pagefile

    Joined:
    Feb 10, 2013
    Posts:
    51
    Declare Start() as virtual protected (or public), and in the child classes override it and call base.Start()
    Code (csharp):
    1. class BaseClass : Monobehaviour
    2. {
    3.     public virtual Start()
    4.     {
    5.         Debug.Log("BaseClass.Start()");
    6.     }
    7. }
    8.  
    9. class ChildClass : BaseClass
    10. {
    11.     public override Start()
    12.     {
    13.         base.Start();
    14.         Debug.Log("ChildClass.Start()");
    15.     }
    16. }
     
    unity_9g8HjFTtWxrLjQ likes this.
  3. Antoids

    Antoids

    Joined:
    Apr 11, 2015
    Posts:
    12
    Thank you for the help. It pointed me in the right direction. However, some small alterations led me to something more like this:

    Code (CSharp):
    1. public class Base : MonoBehaviour {
    2.  
    3.     public static void Start() {
    4.         Debug.Log("base");
    5.     }
    6. }
    7.  
    8. public class Child : Base {
    9.  
    10.     public void Start() {
    11.         Base.Start();
    12.         Debug.Log("1");
    13.     }
    14. }
    Using the version you posted (after adding void before both Start()s), threw an error that it required an object reference for Base.Start(), since it was non-static. I see no reason not to make it static, however, so I did so.
     
  4. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    7,073
    Static is different.What you're looking for is:
    Code (CSharp):
    1. public class BaseClass : MonoBehaviour {
    2.  
    3.     public virtual void Start() {
    4.         Debug.Log("baseClass");
    5.     }
    6. }
    7.  
    8. public class Child : BaseClass {
    9.  
    10.     public override void Start() {
    11.         base.Start();
    12.         Debug.Log("1");
    13.     }
    14. }
    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/base
     
    Last edited: Apr 6, 2018
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,713
    You probably got confused because your class was named 'Base' where 'base' is a keyword inside the class, meaning its parent.. :)
    So, if you typed "Base.Start" you would have that msg.

    Notice the casing of the letter 'b'.
     
    bezier and AbleArcher like this.
unityunity