- Home /
Inventory Storing a Prefab Reference or a Prefab Path as string
Hi all,
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.
Thank you
Your answer
Follow this Question
Related Questions
Instantiate a destroyed GameObject by its original Prefab from the "Assests" folder? 0 Answers
Adress canvas group within a clone of prefab 2 Answers
Object Pooling Script for Autorunner? 1 Answer
Whats the best way to populate a Dictionary with multiple Prefabs? 0 Answers
How to get items(my custom class) by a custom id I paired them to? 1 Answer