C# SubClassing and built in functions!?!

Hey guys! I had some quick questions about subclassing in C# and unity’s built in methods. I currently am having what I imagine is a fairly common issue where I have enemies and players with some similar component behaviors that I need to extend the base functions of slightly in order to tailor them specifically to what I want them to do. Seems like the perfect time to implement sub classes! So I went ahead and gave it a try and got everything working save for one thing. Code in the Update function of the base class does not seem to run at all :frowning:

I tried messing around in the base class and turning the Update method public virtual but that caused compiler errors when I tried to override it. I was thinking about maybe trying to make the Update method in the base class a coroutine and then start that coroutine from the subclass? i figured before I dug down that rabbit hole I would come on here and post some code and see what people thought. First off here is the base class.

using UnityEngine;
using System.Collections;

public class ATBGovnah : MonoBehaviour {
	public bool turnActive;
	public float ATBProgress;
	public float ATBspeed;

	void Update () {
		if (!turnActive) {
			ATBProgress += Time.deltaTime * ATBspeed;
			if (ATBProgress >= 1) {
				setTurn();
			}
		}
	}
	
	public virtual void setTurn () {
		turnActive = true;
	}
}

and here is the subclass I am using to extend that class

using UnityEngine;
using System.Collections;
[RequireComponent(typeof(AudioSource))]

public class playerATBGovnah : ATBGovnah {
	public AudioClip ATBDing;
	public UISlider ATBbar;
	
	void Start () {
		ATBbar = transform.FindChild("UIPanel").FindChild("ATB Progress Bar").GetComponent<UISlider>();
		ATBbar.sliderValue = 0;
	}
	
	void Update () {
		ATBbar.sliderValue = ATBProgress;
	}
	
	public override void setTurn () {
		audio.PlayOneShot(ATBDing);
	}
}

How should I go about doing this? What is the correct way to over ride the built in unity functions? Should I just make the Update code function of the base class a separate method and call it in the update function of the subclass?

The built in methods are unique to each class , inheritance has no affect on them . These functions are only called by the internal engine if the class has been attached to a GameObject in the scene.

So if I wanted code in the base class executed in update the best way is to place it in a method and then just call that method at the start of the update class in the subclass?

That will work , I cant voucher that its the best structure though.

I may misunderstand this on a fundamental level but can I not just “amend” code into methods from the base class? For example when I do

	protected override void setTurn () {
		audio.PlayOneShot(ATBDing);
	}

am I amending the code inside the method to the base code in setTurn in the base class or am I overriding the entire base setTurn method? The latter appears to be what’s going on, but not what I want. Do I not want to override the method if I want to preserve the functionality of the code originally in the method while just extending new functionality?

When you use ‘override’ you do exactly that - override the base method and replace it with another one.

You can call the base method from inside the override though…

protected override void setTurn()
{
        base.setTurn();

        audio.PlayOneShot(ATBDing);
}