How do I inherit an OnTrigger method?

I thought I had all bases covered, here’s my code.
When I have the item script attached it works, but when I have the weapon script attached it doesn’t. Why doesn’t the law of inheritance apply here?

public class item : MonoBehaviour {

    public GameObject Inventory;
    protected Inventory inventory;
    private int itemid;

    protected void OnTriggerEnter2D(Collider2D other){
        inventory.BagItem (this.gameObject);
    }
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class weapon : item {

    private int ATK, Weight;

    void Start () {
        ATK = 10;
        Weight = 20;
    }

}

when I walk into the trigger it says NullReference exception

Correct me if I’m wrong, but I’m sure Unity is built in such a way it doesn’t really accommodate for (and doesn’t promote) inheriting from classes that inherit monobehaviour. I could be wrong but I’ve always been pushed away from it myself.

You may be better off taking a different approach. You could always look at SendMessage()?

So what you’re saying is for every child class of item I need to put it’s own void OnTrigger method?

Ah I see, maybe I should just make a new separate script for all “baggable” items.

that’s pretty much what the component-based philosophy is about.

Edit: this probably is a misunderstanding. After reading again it seems you are intending to inherit the “baggable item” script from the “item” script.

I initially thought we were intending to create a “baggable” script which adds behavour ontop of the “item” script while in the same gameObject.

yes, I don’t really see the logic of using inheritance if I end up having to attach “both” scripts anyway. it would be “kind of” nice to have all items in the game inherit from the “baggable” script. It’s one less drag and drop routine.

but the method is derived from monobehaviour and apparently can’t inherit an OnTrigger? I watched a Unity tutorial and they said It’s better to have multiple scripts, each dedicated to only one task. So I guess following that logic this is better in the long run.

oh thought you can do this:

protected virtual void OnTriggerEnter2D(Collider2D other){
    //stuff
}

and in your inherited script, if you need to add behavior:

protected override void OnTriggerEnter2D(Collider2D other){
    base.OnTriggerEnter2D(other);
    //new stuff
}
7 Likes

You can still have this in one class, if it is just the picking up of things. I did it with seriable classes. Of course this is just one solution and if you are planning on having some bunch of items, you maybe better go with Asset Libraries.

[System.Serializable]
class Test : System.Object
{
    public int p = 5;
    public Color c = Color.white;
}

what the hell?

no OnTriggerEnter works just fine with inheritance. the fact that unity uses Magic methods doesn’t affect this. for instance I have one class that uses OnValidate and virtual OnEnable in a base class, and my inherited classes benefit from the base class’s OnValidate with no issues.

@spritesobhan look closer at your code. the reason it didn’t work is because you left a null reference in. pretty sure you didn’t post the full code so I can’t point it out. but it looks like you never created an inventory instance

Gonna second Joshua, inheritance works perfectly fine with OnTrigger-methods.

You just never created the Inventory. You would nullreference even if you just used the Item MonoBehaviour.

This is absurdly confusing, by the way:

public GameObject Inventory;
protected Inventory inventory;

Like what is going on here. Why do you have two inventories? Why is a gameobject an inventory?

1 Like

Well the reason I was using Inheritance was so I wouldn’t have to rewrite the function in every subclass script anyway, it’d be kind of annoying to do that for each “weapon” “helmet” “armor” “potion” script… etc etc.

I did, when you attach a subclass script, in my case “weapon”, in the inspector it will show its parent class variables, and I attached Inventory to the weapon script, and also the part that I left out here was a

    void Start () {
        inventory = Inventory.GetComponent<Inventory> ();
    }

so I saw no reason for it not to work =/

The reason I was confused why it wasn’t working. But at the time my script did have it, I just didn’t post it here

public class item : MonoBehaviour {

    public GameObject Inventory;
    protected Inventory inventory;
    public string ItemType;
    private int itemid;

    protected void OnTriggerEnter2D(Collider2D other){
        inventory.BagItem (this.gameObject);
    }



    // Use this for initialization
    void Start () {
        inventory = Inventory.GetComponent<Inventory> ();
    }

it only works when I attach both =/

And just to not leave you guys hanging, I tried the same thing again today and it worked. Hmm…

Sometimes Unity is buggy and problems get fixed when I restart the application, or it was really late last night and I missed something… But I was 100% sure I had all bases covered…

Ohh, maybe it’s because I had a void Start in my subclass script? that was overriding the parent-class Start() function?

I just tested this and this was the reason x_x

1 Like

If it’s a null ref exception, you can literally find the cause exactly the same way every single time. Just debug the values until you find the null one, then work your way back through how the variable gets populated and why it isnt.

1 Like

That sounds right. Make the base class’s Start method virtual and override it if you want to add functionality (or not include the parent’s, etc…) :slight_smile: