Quantcast
Channel: Add onto method in parent class from inherited class - Stack Overflow
Viewing all articles
Browse latest Browse all 4

Answer by Jakub Lortz for Add onto method in parent class from inherited class

$
0
0

You can use base to call the base class' implementation of Start(). You also have to override the virtual method in the child class:

protected override void Start () {    base.Start();    BlahBlahBlah();    Stuff();}

You can also use template method pattern to define the skeleton of the method in the base class and provide implementation of some parts in derived classes.

public void Start() {    MyRigidBody = gameObject.GetComponent<Rigidbody>();    if(Controls.Length < 4)        throw new System.Exception("Controls not assigned correctly");    DoSomethingElse();}protected virtual void DoSomethingElse(){    //can be empty in base class, derived classes provide the implementation}

The Start method is not virtual. If you want to modify the behavior of Start method in derived classes, you override DoSomethingElse().


Viewing all articles
Browse latest Browse all 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>