Optimal way to instantiate a random object

I’m trinyg to make an item spawn when I open a chest in my game, but don’t know which route to go to keep it best practice / optimized.

My problem is how should I reference all my items (scriptable objects), do I have to store in memory every scriptable object all the time (Resources.Load everything in a static class maybe?)

Is this the way to go?

maybe you can make another scriptable object that holds a list or an array of them

you definitely need some way of registering all the items (likely prefabs) beforehand in some sort of a pool.

in that sense, zengekku’s advice is a good one. I mean it depends on your exact codebase and other practices in your code, but using SO for such cross-references and dragndrop registration in edit time is definitely preferable in Unity. you can even add a GetRandom method directly in SO.

now from the top of my head, I don’t know if the referenced items would need all to load completely during runtime, and how would you control this behavior if you used AssetBundles instead, or if you didn’t need all of the referenced items. that would definitely need testing, but it’s not too hard to try and prototype various approaches.

Thank you for your responses, I am still new to this, what I was thinking of doing was to create my objects as ScriptableObjects (fast to create, weighs almost nothing) have a prefab for each different type, and give this prefab the ScriptableObject when I instanciate it to have the good result.

From what I understand, your proposition would be to create englobing ScriptableObjects like a SO for enemies, another for equipment etc and then when I need to create something I can use this englobing SO to find a random in a category?

Did I get it right? This would create smaller batches to load on memory so I guess it could be a good idea, I will see how to implement it.

Also, I don’t know about AssetBundles, do you have a quick explaination of what it does?

Yes. That way you have meta definition so to speak, full of references, with an added benefit of automatic integration with the editor, because you can dragndrop your references and not think of how they’re actually resolved in realtime. No ids, no enums, no collision, no opportunity to mess up or break something if you want to change any of this, no nothing.

AssetBundles are used to delimit your resources into non-monolithic pieces that can either load dynamically or load from elsewhere (think online). Not really that important, but it’s worth investigating depending on your target platform or amount of resources you intend to use. Don’t bother too much if you’re new, and especially if you’re targeting desktop.

In your particular case I just got confused for a moment how Unity would handle a cross-referencing all-encompassing ScriptableObject and I’m still not really sure. It should either demand referenced resources immediately, or blow up, or even fail silently (or crash on call), and not a single of these outcomes are satisfactory to me. But I don’t know enough about this area of Unity to comprehend exactly how it should behave.