I am looking into making a simple inventory system. I am new to Unity.
I understand that storing the gameObject themselves is not good practice.
I was thinking about two approaches:
1) Each item has an ID attached to it, when the item is collected the ID is added to a list, when the item is needed in the world again the inventory has a method to instantiate the object using a dictionary.
2) Each item has a static variable which is the string to the prefab (e.g. "Resource/Assets/happyCarrot"). When the item is collected the string is added to a list, when the item is needed again the item can be generated using the Load method and the string from the list.
I can see number 2 as easier to implement and easier to read, as each item will have a clear path rather than a number ID. Also, since I can't create a dictionary in the inspector I will have to create the dictionary in a script and on the start populate it with a for loop through a list/array that I will have populated with all the prefabs in the inspector. Therefore the first item in the array will be given an ID of 1 etc ...
Also, will storing things using a Dictionary actually pass a reference to that prefab or the actual prefab itself in which case is it any better than storing gameObjects.
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
3 People are following this question.
How to use ScriptableObject for Inventory System
Accessing Vuforia ARCamera.prefab position from a script
How do you destroy a prefab, but not the original object?
webplayer serialize/deserialize error advice plz
Modifying scene objects programatically references prefab