- Home /
Scriptable objects as inventory items unable to save reference
I recently implemented scriptable objects as data container for inventory items. Upon collecting an item a reference to the scriptable object is stored along with information specific to that item instance such as number stacked and durability.
This system works well until I try to save the inventory to be loaded later. Its trivial to save dynamic information number stacked and durability. It does not seem possible to directly save a reference to scriptable object. I'm wondering if anyone has any idea for a work around? Or if I just need to give up on this route and remove scriptable objects from my inventory system?
As a side note I'm a disappointed that on both Unity's scriptable object tutorials the basic use case is for inventory items because its extremely limited if a reference to the scriptable object cannot be saved.
links to tutorials: https://unity3d.com/learn/tutorials/topics/scripting/overthrowing-monobehaviour-tyranny-glorious-scriptableobject
https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/scriptable-objects
Answer by Brrokken · Oct 23, 2016 at 09:08 AM
After trying a couple more things I came to workable solution, and now see how its possible save and load a reference to a scriptable objects . Heres my solution.
Add unique ID to a scriptable object. Store a list/array of scriptable object on your script. Save the unique Id to file using player prefs or binary ( https://unity3d.com/learn/tutorials/topics/scripting/persistence-saving-and-loading-data) When loading iterate through scriptable object list and set correct references.
Although it be easier to save a reference to a scriptable object this isn't too much extra work, so I'm rejoining the glorious scriptable object revolution.
Can you please tell me how and where you stored the list with scriptable objects and how to save the id in the binary script?
Cant seem to figure this out. I have added an unique id to all items.