Inventory / inhertance driving me insane - help with theory please.

Ok, I am trying to make an inventory system and I am going in circles. I think if I add one more GetComponent call into my code my computer might burst into flames immedietely. I thought about just stealing one but I really want to understand this. This thread isnt really about how to make the actual OnGUI or anything its about the inheritance and setting up the scripts and the code.

I am not asking for code I just need to understand the flow, and get it through my apparently thick head.

So what I have so far is this:

First I have a script(class) called Item. This script outlines the basic features of an item: Name, cost, description. Ok so far so good.

If I understand correctly, I can then have other scripts, weapon for example, that inherit from item and further give more qualities of the item - damage, for example. The item then also inherits the basic traits, cost, name description which I set in the item script I believe? Ok I get that I think.

So now, I have a script called “Item Manager” which is where I create my list of Items: public List PlayerInventory = new List(); And so far I have been putting the functions in here for adding to this list. So for example, if I click a button or something it adds a new Item to the inventory list and sets the parameters: Basically like this:

Item newItem = new Item(); = new ItemName; // Declared at top of this script.


I guess this is where I get lost:

My biggest question is this:
If I have a gameObject in my scene that has a script on it that does a bunch of stuff, how can I set this to be of a type weapon, for example or of type Armor? For example, I have an arrow that gets fired in my scene by the player. It has a script that does a bunch of stuff so I feel like I cant make it inherit from the Item script or all that wont work(because its not inheriting from mono). How do I set it to be of type: Item or type: weapon?

When I then click on the gameObject, how can I tell what kind of object it is? I guess that depends on the answer to the above. I “think” if I can somehow make it inherit from “item” I can use gameobject.GetType I believe? If I cant inherit though I have no idea. I tried to set a variable string called type on the object and then access that but thats making no sense and obviously the wrong way to go about it.

How would you recommend adding gameObjects into the List(Player Inventory)? Right now I have a empty gameobject which holds the script ItemManager which adds the items. Should I just access those AddItem functions using GetComponent to add the Item when my player clicks on an object? Or should I put the script on the player themselves? I see everyone using the empty gameobject with a “inventory manager” of sorts and I am not sure why they go this direction.

Whew I am rambling here I guess I just need some direction on how to manage all this! Feel free to direct me to a link that describes this flow but I havent found anything yet :frowning:

A gameobject is a gameobject. A component is something you can add to a gameobject to add some behaviours. This is the part you can control, and use inheritance on. Here is a sample of code. The function ‘GetYourObjectOnCLick’ is just there to simulate the way you are getting the gameobject.

GameObject obj = GetYourObjectOnCLick();
Item item = obj.GetComponent<Item>();

if(item.GetType() == typeof(Weapon))
// do your stuff there
else if(item.GetType() == typeof(Ammo))
// do other stuff there
// do something else