I’m setting up my inventory system and I need to call a certain method depending on the type of my item class.
Well, obviously you would think of a Get Type check, but here is the thing :
I’m getting a reference of my currently selected object in a VARIABLE of type BaseItem (which is my base class for all my items/weapons/equips…) so the same variable can manage all the subclasses.
Because of this settings, when I try to check what is the Type of my currently selected object, Get Type can’t return any of my subclasses cause they are “stored” as a BaseItem.
At least, that’s how I understand my problem…
What solution do I have ? Setting up an Enum (item-weapon-whatever) in my BaseItem class ?I suppose that would do the job but I’d like to get around this sort of “type-cast” issue !
GetType will return the type of the instance and not the type of the variable that’s holding it.
However, unless your base class derives from ScriptableObject or MonoBehaviour, Unity serializes fields by their field type and not the type of the instance it’s holding. So it could be that all of your items are getting serialized as BaseItems.