List of a type of SO

Okay, so scriptable objects confuse me because they sit in the editor and I don’t know how to use them like I would a gameobject or any old variable. I have scriptable objects of type ‘Fish’ and in my fishing minigame I need to reference all fish (really location will determine which of these fish are possible but just to get all of them would be nice for now). Within my minigame script, how can I iterate through all of these scriptable object Fish so that I can choose a random one? Or maybe a better question is how do I generate a ‘List’ in code consisting of all the scriptable objects of type ‘Fish’?

A few options:
public List<Fish> allFish; Then just drag and drop all the fish from your project window into this list.
You can also throw all the fish SOs into a single folder and use Resources.LoadAll<Fish>(foldername) to grab them all. See Unity - Scripting API: Resources.LoadAll
You could also use Addressables in a similar way.

And then there’s more exotic approaches like writing custom editors or using plugins like Odin Inspector to prepopulate lists of Fish, etc.

1 Like

The above is excellent advice. To add…

They are like any Unity asset that lives in your project files. You can serialise a field of it’s type and assign an asset via the inspector. Similar to materials, meshes, and even prefabs. Main difference being they are assets you can code your own behaviour into (which is awesome).

Thank you guys. I did this after asking while I awaited an answer and it worked out nicely. I will now have to use their location to determine if the current scene could randomly choose said fish (Like if you are on a beach, it should never randomly choose bass as they don’t live in ocean)

You could go one step further! Make scriptable objects that act as small ‘pools’ (pun intended) of fish to choose from. That way you can set up appropriate collections of fish and just reference these loot table Scriptable objects where you need them instead.

You could even design them so you can add in how frequent they appear, etc. See where I’m going?