Issue with Base and Derived Class, Field is null when called with base keyword.

Here’s what I’m trying to do:

I have a base class (Base) and a derived class (Derived).

Base has a field of TypeA called SomeType.
Derived has a field of TypeB called SomeType.

TypeA is the base class of TypeB.

I wanted TypeB from Derived to override TypeA from SomeType.

When I call the base virtual method from the derived override method, SomeType is null, indicating that it is still using the base.

Here’s the relevant class architecture:

namespace RPGCharacterAnims
{
    public  class RPGCharacterInputSystemController : MonoBehaviour
    {
        protected RPGCharacterController rpgCharacterController;

...

      protected virtual void Moving()
        {
            moveInput = new Vector3(inputMovement.x, inputMovement.y, 0f);

            // Filter the 0.1 threshold of HasMoveInput.
            if (HasMoveInput()) { rpgCharacterController.SetMoveInput(moveInput); }
            else { rpgCharacterController.SetMoveInput(Vector3.zero); }
        }
...
   }
}
namespace RPGCharacterAnims
{
    public class MyCharacterInputSystemController : RPGCharacterInputSystemController

        [SerializeField]
        protected MyCharacterController rpgCharacterController;
...
        protected override void Moving()
        {
            base.Moving();
        }
...
   }
}

This produces a null when checking for the rpgCharacterController in the base method, but the correct object in the derived method.

I understand that pulling from a base that uses a different class can have issues, and is not ideal.
I’m open to a better way to structure this.

[EDITED: To remove [Serialize Field] from the field in Script A.]

Why are you redefining the variable in the child class? That is hiding the one in the parent, and causing your entire issue. Delete line 8 in your second script.

1 Like

Thanks!

While messing with the code architecture I overlooked this.

Quick question, I accidently put in the code where it was serialized in Script A and B.

However, I wanted the field to be Serialized in Script B but not in Script A.

Is it possible to do this?

With inheritance no, as that’s the point of inheritance, that the child inherits the traits of the parent.

1 Like

Ah so the traits of the child cannot be altered once inherited? I thought inheritance could allow modification of the derived field’s properties once inherited.

I can just make the parent Serialized for my own purposes, but I wanted to avoid altering the parent.

I’m extending a different author’s script and bridging it’s compatibility. I felt this would be useful for others should I want to share it. If I have to alter the base class, that drops its plug and play capability.

Part of the change requires me to make the field assignable in the Unity Editor, since the base class uses GetComponent, but I wanted to make it so the Component doesn’t have to be on the GameObject.

(In this case, I want to separate the Character and Player, whereas the script demands that the Character and the Player be one GameObject)

Is there another way to do this?

You can override methods and properties that are marked as abstract or virtual. But fields belong to their bodies as-is, and just have different access modifiers.

Whether a member is marked private, protected, public, etc, they still in exist in both parent and derived types, regardless of whether they’re overridden or not.

If the base class doesn’t allow you to override this behaviour, you’re SOoL and just need to make your own controller.

1 Like

this can also happen if you are working with objects that are marked with monobehaviour in the parent class but not supposed to be…