How to reference a Collection in script

I am using ScriptableObject scripts to organize certain groups of objects in my game, and I ran into a problem. I have to always select the AssetMenu collection inside the editor to reference it in a script. I can’t do that to all scripts because some of them are loaded in-game and I can’t change their values unless inside the script itself.

How do I make so the script searches for the AssetMenu in the assets folder to reference it?

I’m not sure of your question… do you mean putting in something like this in the Project folder:

t:AssetMenu

? That will work for ScriptableObjects but AFAIK it does not work for prefabs, even if they contain such a script.

Or do you mean loading these at runtime, like with

var menus = Resources.LoadAll<AssetMenu>( "MyAssetMenusFolder/");

I mean that I have public ExampleCollection CollectionScript = and I want to know what to put at the other side of the equation.

How do I make so it can load the whole collection from assets?

Can it get the collection from files?

How do I do this?

“Collection” => Collections - C# reference | Microsoft Learn

So what you write here makes no sense to me.

It has been a while I made this thread so I don’t really know why I wrote collection if that isn’t correct, but in any case what I meant was that I need to reference a ScriptableObject object in code instead of dragging it in the editor.

If you don’t want to drag stuff into the inspector, your other option is to use one of these methods to load it at runtime:

Pay VERY close attention to the requirements and sample code or it will not work.

1 Like

If you are talking about an editor extension that assigns the variable, you probably want to use AssetDatabase.LoadAssetAtPath.

1 Like