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();
newItem.name = new ItemName; // Declared at top of this script.
//etc
//etc
PlayerInventory.Add(newItem);
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