Why is Start() from my base class ignored in my sub-class?

I fully understand that I’m probably making an error, but I’m not sure how to rectify it.

I’m only not posting code because it’s easily described; make two scripts, make one be a standard script and the second one a sub-class of the first script. If you then attach the sub-class to a game object, Start() from the base class doesn’t fire. The Start() only fires if you attach only the base class to the object, verifying that the Start() function itself does work.

How can I fix this?

declare the Start method public virtual in base class and public override in all classes derived from it, methods without access specified are considered private

Start in your base class is not public so when you extend the class start is hidden. You will have to implement a new Start method and call base.Start(). That is what we do on our project.

I am also assuming that your coding in C#.


EXAMPLE:

public class TourGuide : _Model {
new void Start() {
base.Start();
transform.Rotate(Vector3.down,95.5f);
}
}

public class _Model : MonoBehaviour {
void Start() {
// original code.
}
}

I believe that, basically, Unity only calls the lowest implementation of any message function (which Start() is), so the sub-class will always have its version called and not the parent class. Compare this to inheritance in a lot of OOP languages in general, and this is also the case (the overriding version is the one called). Note, that this is not overriding the Start() method, rather it is shadowing the name.

I’m not sure how to do this in Javascript, but with C# you can declare the base class’s Start() like this:

protected new void Start() { /* ... */ }

… and then call it from the sub-class by using base.Start(). I personally don’t like this solution, because it’s not really clear what’s going on there, so you can do something more like this:

// Base class
void Start() {
    DoInitialization();
}

void DoInitialization() {
    // Base class's code goes here
}

// Subclass
void Start() {
    base.DoInitialization();
    // Other init code
}

One question you should ask yourself, though, is sub-classing what you really want to do? If you’re just looking to force some shared functionality, why not just use add the base class as a component to the game object you’re working with? You can even force it with a RequireComponent(). That way you can still get the functionality of what was the base class without the funkyness of using inheritence in MonoBehaviours.

I don’t know if something in Unity changed in this regard since this was asked / discussed but I just did exactly what the OP wanted and it worked.

If you declare a base class abstract (inherited from MonoBehaviour) and put a child class on a gameobject (in the scene) and implement Awake() or Start() in the base, the child class doesn’t need to do anything (except for inheriting of course). When the scene starts these callbacks get called on the base class automatically. I am using Unity v3.5.7.

For those looking for code:

In the base class:

public virtual void Start()
{
    // base class code runs
}

In the class that inherits from it:

public override void Start()
{
    base.Start(); // runs the code from the base
    // add your additional code here
}