Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly inherit Unity's callback functions like Awake(), Start() and Update and FixedUpdate()?

I've created a parent class that I expect to have all functions related to testing if the GameObject is grounded, in water, on air, etc... given that this functions will be used by the player as well as other GameObjects. However, the child class seems not to inherit properly the functions.

The parent script it's as follows:

public class CharacterPhysic : MonoBehaviour {
  [SerializeField] protected Transform groundPoints;
  float grounRadius;
  private void Start ()
  {
     whatIsGround = LayerMask.GetMask("Ground");
     groundRadius = 0.01f;
  }
  protected bool IsGrounded()
  {
     Collider2D[] colliders = Physics2D.OverlapCircleAll(groundPoints.position, groundRadius, whatIsGround);
     if (colliders.Length > 0)
     {
         return true;
     }
     else return false;
  }
  private void FixedUpdate()
  {
     Debug.Log(IsGrounded());
  }
}

And the children script is just:

public class ErrantMove : CharacterPhysic {
  private void FixedUpdate()
  {
     Debug.Log(IsGrounded());
  }
}

When added the first script as a component to a Gameobject (after defining the grounPoint) the Debug.Log(IsGrounded()); returns TRUE

However, when added the second script as a component to the same Gameobject (after defining the grounPoint and remove the first script) the Debug.Log(IsGrounded()); returns FALSE even in the same circumstances.

I'm expecting to be able to add movement functions into the second script but this depends on the ability to test if it is grounded.

like image 906
Rodrigo Gonzalez Florian Avatar asked Dec 23 '22 03:12

Rodrigo Gonzalez Florian


1 Answers

You can properly inherit Unity's callback functions such as Awake, Start and Update like you would with a normal C# inheritance paradigm. It's very simply.

Make the all the callback functions for the base class to be virtual:

public class YourBaseClass : MonoBehaviour
{
    protected virtual void Awake()
    {
        Debug.Log("Awake Base");

    }

    protected virtual void Start()
    {
        Debug.Log("Start Base");
    }

    protected virtual void Update()
    {
        Debug.Log("Update Base");
    }

    protected virtual void FixedUpdate()
    {
        Debug.Log("FixedUpdate Base");
    }
}

For the parent class that will derive from the base class, add the callback functions too but mark them as override. If you need the base function to be called, call it with base.FunctionName before doing anything else in the parent function:

public class Parent : YourBaseClass
{
    protected override void Awake()
    {
        base.Awake();
        Debug.Log("Awake Parent");
    }

    protected override void Start()
    {
        base.Start();
        Debug.Log("Start Parent");
    }

    protected override void Update()
    {
        base.Update();
        Debug.Log("Update Parent");
    }

    protected override void FixedUpdate()
    {
        base.FixedUpdate();
        Debug.Log("FixedUpdate Parent");
    }
}
like image 150
Programmer Avatar answered Dec 30 '22 11:12

Programmer