Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How do I make child class add to parent class's functions instead of overriding them?

Discussion in 'Scripting' started by gmfbrown, Jul 10, 2022.

  1. gmfbrown

    gmfbrown

    Joined:
    Jan 9, 2019
    Posts:
    25
    I'm trying to do some procedurally generated geometry where specific types of shapes are child classes of a more general Shape2D class. However, when a function is defined in both the parent and child class, the code in the child class overrides whatever is in the parent class. For instance, in the Shape2D class, I have the following in the Update method:

    Code (CSharp):
    1.     void Update()
    2.     {
    3.         GenerateMesh();
    4.     }
    And in the Rectangle child class I have the following:

    Code (CSharp):
    1.     void Update()
    2.     {
    3.         vertices = new List<Vector3>()
    4.         {
    5.             new Vector3(-width / 2,  height / 2),
    6.             new Vector3( width / 2,  height / 2),
    7.             new Vector3(-width / 2, -height / 2),
    8.             new Vector3( width / 2, -height / 2)
    9.         };
    10.     }
    I put that code in the child class so that, if the Rectangle class changes its dimension, it will update the shape. However, by having code in the Update method of the child class, it overrides the GenerateMesh call in the Update method in the parent class, preventing the mesh from ever being drawn in the first place. I can fix the problem by repeating the GenerateMesh call in the child class as well, but that defeats the purpose of using parent and child classes.

    What do I need to do so that Update will do everything in the parent class and the child class, without having to rewrite all the parent class code in each child class?
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    You can call the parent function from a child function using the base keyword:
    Code (CSharp):
    1. void Update()
    2. {
    3.   base.Update(); // Run parent Update function.
    4.  
    5.   vertices = new List<Vector3>()
    6.   {
    7.     new Vector3(-width / 2,  height / 2),
    8.     new Vector3( width / 2,  height / 2),
    9.     new Vector3(-width / 2, -height / 2),
    10.     new Vector3( width / 2, -height / 2)
    11.   };
    12. }
     
  3. gmfbrown

    gmfbrown

    Joined:
    Jan 9, 2019
    Posts:
    25
    Thanks! That mostly solved the problem. In case anybody has a similar question and stumbles across this post I will add the following steps I needed to take:

    1. In the parent class, make Update have a status of "protected".
    2. In the child class, make Update have a status of "new"

    The resulting functions now look like this:

    In the Shape2D class
    Code (CSharp):
    1.     protected void Update()
    2.     {
    3.         GenerateMesh();
    4.     }
    In the Rectangle class
    Code (CSharp):
    1.     new void Update()
    2.     {
    3.         vertices = new List<Vector3>()
    4.         {
    5.             new Vector3(-width / 2,  height / 2),
    6.             new Vector3( width / 2,  height / 2),
    7.             new Vector3(-width / 2, -height / 2),
    8.             new Vector3( width / 2, -height / 2)
    9.         };
    10.         base.Update(); // Run parent update function
    11.     }
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,883
    I don't think you want to use the new keyword here. Doing so hides the base implementation and has ramifications with polymorphism. Ergo, if you have this child class stored in a field of the type of its base class and call Update() (not something you'd normally do, just an example), the base implementation will be called, not the overridden method.

    You want to do the following:
    Code (CSharp):
    1. //base class
    2. protected virtual void Update()
    3. {
    4.     //logic
    5. }
    6.  
    7. //child class
    8. protected override void Update()
    9. {
    10.     //logic before base implementation
    11.     base.Update();
    12.     //logic after base implementation
    13. }
     
    Last edited: Jul 13, 2022
    Vryken likes this.