- Home /
Searching and loading a specific instance of a Scriptableobject from the editor by its name
Hi! I've been trying to do this with Resources.Load<>();
but to no avail. this is my ScriptableObject.
[CreateAssetMenu(fileName = "New Move", menuName = "Move")] public class Move : ScriptableObject { public enum Type{Water, Fire, Ice, Light, Dark, Kami}; public bool isDamaging; public bool isUsable = true; public int dmg; public int reloadTime; public GameObject spawn; public Type type; }
I'm not very experienced with scripting, I'm rather a beginner, so some help would be appreciated. How can I search for a specific instance of it sitting in the editor in my Assets folder through script?
If the code is hard to read, here's a more organized version of it:
[CreateAsset$$anonymous$$enu(fileName = "New $$anonymous$$ove", menuName = "$$anonymous$$ove")]
public class $$anonymous$$ove : ScriptableObject
{
public enum Type{Water, Fire, Ice, Light, Dark, $$anonymous$$ami};
public bool isDamaging;
public bool isUsable = true;
public int dmg;
public int reloadTime;
public GameObject spawn;
public Type type;
}
Answer by AltaNox · Jan 07, 2020 at 06:13 PM
Resources.Load<>() it's the correct method. Just remember that the Resources class only loads stuff from a folder in your project called Resources. This is stated in the documentation for Resources.Load. From that point you only need to provide the correct path for the scriptable object.
For example, if the file is just under Resources and it is called FireMove
Move move = Resources.Load<Move>("FireMove");
If the file is under a folder called Scriptables inside Resources
Move move = Resources.Load<Move>("Scriptables/FireMove");
This should do what you want.
As a side note, you should probably take a look at Asset Bundles. These are what you should ideally be using for loading assets at run time. It's a bit more complicated but it is the preferred way to do it since it provides a lot more flexibility. Resources is an easy and fast way to load something but if you want to do anything serious with it, it won't hold up that well. Anyway, maybe for your project Resources is enough but that is up to you.
Thanks! I'll investigate about Asset Bundles for future projects.
No problem :) If the problem is solved please mark the answer as accepted.
Your answer
Follow this Question
Related Questions
Using ScriptableObjects data loaded with the Addressable Package 0 Answers
Instancing ScriptableObject at Runtime, loading and saving via JsonUtility 1 Answer
How to load ScriptableObject from asset file immediately? 1 Answer
Saving items and reading their data 1 Answer
How to Save and Load a list of Scriptable Objects from a File? 2 Answers