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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Correct syntax to access child class from parent class?

Discussion in 'Scripting' started by brigas, May 25, 2020.

  1. brigas

    brigas

    Joined:
    Oct 4, 2014
    Posts:
    522
    I am using inheritance and I have a parent class and a child class:

    Code (CSharp):
    1. public class parentClass : MonoBehaviour
    2. {
    3.  
    4. void Update(){
    5.  
    6. Debug.Log( childvar ); // trying to pull a field from the child class doesn't work
    7.  
    8. }
    9.  
    10. }
    11.  
    12. public class childClass : parentClass
    13. {
    14. public int childvar = 0;
    15.  
    16. }
    of course I could do:


    Code (CSharp):
    1. public class parentClass : MonoBehaviour
    2. {
    3.  
    4. public childClass childref;//assign in inspector
    5.  
    6. void Update(){
    7.  
    8. Debug.Log( childref.childvar ); // this works
    9.  
    10. }
    11.  
    12. }
    13.  
    14. public class childClass : parentClass
    15. {
    16. public int childvar = 0;
    17.  
    18. }
    but is there a better way?
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    A child is always also of the type of its parent, thus you can access the parent functionality, but the other way around simply makes no sense. Generally speaking, requiring to do this is probably related to some architectural design flaw.

    The base class is a generalization and the child class a specialization. It makes sense that a specialization knows about the general concepts involved, but it does not make sense for a generalization to have access to specifics (because why use inheritance then?).
    Or in other words, an Animal does not need to know any of the features a Bird specifically may have.
     
    lordofduct likes this.
  3. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    childvar isn't declared in the parent class, so it can't know about the child's attributes. That's like trying to determine what the child's wearing before it's born.

    Whenever you try top access a child class' attribute from a parent there is a 99% chance that your logic is flawed. What are you trying to solve?
     
  4. brigas

    brigas

    Joined:
    Oct 4, 2014
    Posts:
    522
    for example in my parent class I have in update something like:
    Code (CSharp):
    1. public class parentClass : MonoBehaviour
    2. {
    3. void Update(){
    4. //some movement code valid for all parents and child
    5.  
    6. //SOME ANIMATION CODE ONLY VALID FOR CHILD
    7.  
    8. //some cleanup code valid for all parents and child
    9. }
    10.  
    so i can't cleanly override the update in the child to be
    Code (CSharp):
    1. public class childClass : parentClass
    2. {
    3. override void Update(){
    4. base.Update()
    5. //CHILD ANIMATION CODE // needed to execute before parent update cleanup code
    6.  
    7. }
    8. }
    I need access to the child inbetween the update of the parent.

    Should I just copy paste the update function and fully override it and just cutout the child parts in the parent?

    This gets messy if I need to alter the parent update code, I will also need to go rewrite the override I did on the child
     
    Last edited: May 25, 2020
  5. Cannist

    Cannist

    Joined:
    Mar 31, 2020
    Posts:
    64
    The way this is typically solved in object oriented programming is to introduce a special empty method in the parent that can be overriden in the child.
    Code (CSharp):
    1. public class ParentClass : MonoBehaviour
    2. {
    3. void Update(){
    4.    //some movement code valid for all parents and child
    5.  
    6.    doAnimation();
    7.  
    8.    //some cleanup code valid for all parents and child
    9. }
    10.  
    11. protected virtual void doAnimation() {}
    12. }
    Sometimes it makes even sense to decompose the whole method to enable child classes to override all of the different aspects if they need to.
    Code (CSharp):
    1. public class ParentClass : MonoBehaviour
    2. {
    3. void Update(){
    4.    doMovement();
    5.    doAnimation();
    6.    doCleanup();
    7. }
     
  6. brigas

    brigas

    Joined:
    Oct 4, 2014
    Posts:
    522
    Ah this makes a lot of sense, thank you very much!