Search Unity

how to call parent's Start() method from child in C#?

Discussion in 'Scripting' started by Dark-Table, Feb 20, 2009.

  1. Dark-Table

    Dark-Table

    Joined:
    Nov 25, 2008
    Posts:
    315
    This should be simple, but I can't find documentation for it. I'm trying to call the parent class' Start method from the child's Start method.

    Code (csharp):
    1. public class BlahChild : Blah {
    2.   void Start() {
    3.     parent.Start()
    4.     // other stuff
    5.   }
    6. }
    7.  
    What's the correct syntax for doing this?

    Thanks
     
  2. tolm

    tolm

    Joined:
    Feb 4, 2008
    Posts:
    61
    How about base.Start()?
     
  3. Dark-Table

    Dark-Table

    Joined:
    Nov 25, 2008
    Posts:
    315
    Thanks, that's what I was looking for.
     
  4. markhula

    markhula

    Joined:
    Sep 3, 2011
    Posts:
    630
    How would a grandchild execute it's parent's Start though? (i.e. not base class)???

    Cheers
     
  5. wccrawford

    wccrawford

    Joined:
    Sep 30, 2011
    Posts:
    2,039
    You can't. You can only execute the parent's Start. However, if the parent doesn't have a Start(), it inherits it from the grandparent, and that one will be called instead.

    If the parent has a Start(), you cannot skip over it and run the grandparent's instead.
     
    Bunny83 and WoodsmanThePeasant like this.
  6. markhula

    markhula

    Joined:
    Sep 3, 2011
    Posts:
    630
    !!!!
    But in most OOP you have super/parent/top or something.
    So if I have class A

    and B inherits from A

    and C inherits from B

    then B can do base.start() //which will execute A.start();
    C can do base.start() //which will execute A.start();

    But 'C' can't execute B.start()?????

    Cheers
     
  7. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Correct. You can chain constructors but not methods.
     
  8. markhula

    markhula

    Joined:
    Sep 3, 2011
    Posts:
    630
    Gulp!
    So is this a Unity restriction or a c# in general ??

    Cheers
     
  9. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    thats a general thing
     
  10. markhula

    markhula

    Joined:
    Sep 3, 2011
    Posts:
    630
    Thanks!!
     
  11. rokyed

    rokyed

    Joined:
    Sep 22, 2012
    Posts:
    12
    its not restricted just make shure your methods are public and all the stuff you want to access directly from child example : parent has :
    public void MyMethod(){

    }
    child now sees :
    MyMethod();

    so is all there
     
  12. Deleted User

    Deleted User

    Guest

    Code (CSharp):
    1. class C:B
    2. {
    3.  
    4.    protected new void Start()
    5.    {
    6.       base.Start();
    7.       Debug.Log("C Start");
    8.    }
    9. }
    10.  
    11. class B:A
    12. {
    13.    protected new void Start()
    14.    {
    15.       base.Start();
    16.       Debug.Log("B Start");
    17.    }
    18. }
    19.  
    20. class A:Monobehaviour
    21. {
    22.    protected virtual void Start()
    23.    {
    24.       Debug.Log("A Start");
    25.    }
    26. }
    otuput:
    "A Start"
    "B Start"
    "C Start"

    Please , can I ask for someone to explain why does it works ?
     
    sascharo likes this.
  13. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,914
    Why wouldn't it?

    line 6 calls the method on line 13. Line 15 calls the method on line 22.
     
  14. Hellothere_1

    Hellothere_1

    Joined:
    Sep 18, 2018
    Posts:
    32
    You call C.Start().

    The first thing that C.Start() is call its base version, and since C inherits from B, that's B.Start(). B.Start() respectively calls A.Start().

    A.Start() does its output, then the program returns to B.Start (), which does its output, before returning to C.Start().
     
  15. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    What @benqowny is showing isn't method overriding because he's declaring each derivative Start method as new.
     
  16. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,914
    That's correct. However he's calling
    base.Start()
    which will call the new Start method of the immediate parent class because that is the method with the most imminent(?) scope.
     
  17. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    691
    transform.root.GetComponent<rootScript>().Start();

    Just change rootScript to the name of the script attached to the base object that you want to call Start() or whatever else on.