Unity - Baseclass should get Information from Subclass

Hi,
I am trying to make use of the MVC Pattern in Unity. I am a programming beginner.

Traps and moving Platforms use the same code so i created a base for them. I divide the code into “Data”-class and “Method”-class.

Both objects move to Point A, then to Point B, back to Point A and so on…
Point A and Point B got a trigger, to change the Movementdirection of the Platform/Trap.

The base class holds the data. The subclass gets the data and fills the base data. In the base class i have the object:

public virtual GameObject MovingObject { get { return null; } }

The subclass overrides the property returning null to make it return the right object. I try it this way:

[SerializeField]
        private GameObject movingObject;
        public override GameObject MovingObject { get { return movingObject; } }

The private variable is set in the Editor and sets the value to the property. This property gives the information to the base class. The problem is that i get null references and I do not know how to fix that. The base class does not return an object. The information get lost on their way to the base…

Is my logic wrong?

If you need to see the whole structure of these six classes you can look it up on

https://github.com/Garzec/MidnightFeast/tree/master/Assets/Scripts/MovingObjects

Sorry, i did not want to post all lines of code and unrelevant stuff :slight_smile:

Where exactly does the error get thrown? Because the current snippets are not the source of the error. So please post the complete error, and the complete script the error gets thrown in.

I have this base class

public class MovingObjectsData : MonoBehaviour
{
    public virtual GameObject MovingObject { get { return null; } }

    public Transform MovingObjectTransform { get { return MovingObject.transform; } }

    public Vector3 MovingObjectPosition { get { return MovingObjectTransform.position; } set { MovingObjectTransform.position = value; } }

    public virtual GameObject StartObject { get { return null; } }

    public Transform StartObjectTransform { get { return StartObject.transform; } }

    public Vector3 StartObjectPosition { get { return StartObjectTransform.position; } set { StartObjectTransform.position = value; } }

    public virtual GameObject EndObject { get { return null; } }

    public Transform EndObjectTransform { get { return EndObject.transform; } }

    public Vector3 EndObjectPosition { get { return EndObjectTransform.position; } set { EndObjectTransform.position = value; } }

    public int MovementSpeed { get { return 4; } }

    public Vector3 TargetPosition { get; set; }

    private void Start()
    {
        TargetPosition = EndObjectPosition;
    }
}
public class MovingObjectsController : MonoBehaviour
{
    MovingObjectsData data;

    private void Start()
    {
        data = GetComponent<MovingObjectsData>();
    }

    protected void MoveObjectToPoint()
    {
        data.MovingObjectPosition = Vector3.MoveTowards(data.MovingObjectPosition, data.TargetPosition, data.MovementSpeed * Time.deltaTime);
    }

    protected void TriggerActivated(Collider col)
    {
        if (col == data.StartObject.GetComponent<BoxCollider>())
        {
            data.TargetPosition = data.EndObjectPosition;
        }
        else if (col == data.EndObject.GetComponent<BoxCollider>())
        {
            data.TargetPosition = data.StartObjectPosition;
        }
    }
}

Followed by the Platform

public class PlatformData : MovingObjectsData
{
    [SerializeField]
    private GameObject movingObject;
    public override GameObject MovingObject { get { return movingObject; } }

    [SerializeField]
    private GameObject startObject;
    public override GameObject StartObject { get { return startObject; } }

    [SerializeField]
    private GameObject endObject;
    public override GameObject EndObject { get { return endObject; } }
}
public class PlatformController : MovingObjectsController
{
    MovingObjectsData data;

    private void Start()
    {
        data = GetComponent<MovingObjectsData>();
    }

    private void Update()
    {
        MoveObjectToPoint();
    }

    private void OnTriggerEnter(Collider col)
    {
        TriggerActivated(col);

        if (col.tag == "Player")
        {
            col.transform.parent = data.MovingObjectTransform;
        }
    }

    void OnTriggerExit(Collider col)
    {
        if (col.tag == "Player")
        {
            col.transform.parent = null;
        }
    }
}

And the Trap:

public class TrapData : MovingObjectsData
{
    [SerializeField]
    private GameObject movingObject;
    public override GameObject MovingObject { get { return movingObject; } }

    [SerializeField]
    private GameObject startObject;
    public override GameObject StartObject { get { return startObject; } }

    [SerializeField]
    private GameObject endObject;
    public override GameObject EndObject { get { return endObject; } }

    public int CollisionDamage { get { return 20; } }
}
public class TrapController : MovingObjectsController
{
    private void Update()
    {
        MoveObjectToPoint();
    }

    private void OnTriggerEnter(Collider col)
    {
        TriggerActivated(col);
    }

    private void OnCollisionEnter(Collision col)
    {
        if (col.gameObject.tag == "Player")
        {
            //   ChangeValue(-data.CollisionDamage);         ->          data.CurrentHealth - collisionDamage;
        }
    }
}

So when i fill the data in the subclasses with the right objects, the base class “MovingObjectsData” say, the EndObject is null. But the subclasses got this object correct. A NullreferenceException appears for the Trap and for the Platform 1 time.