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()?
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.
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;
}
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
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
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> ();
}
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…
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.