- Home /
Loading ScriptableObject when I open Unity
Hello there,
I'm trying to make a database of VFXs using a scriptable object.
I need this database both at Runtime and in the Editor. I need it in the editor because the VFX picker script I made goes through the database and creates a popup in the inspector with the full list of all the VFX currently present in the database.
My problem is that when I load Unity and select the gameobject with the VFX picker, I get a NullReferenceException. When I manually click on the ScriptableObject databse in the Project window, the error goes away and everything works fine.
The problem is that I have no idea how to load this ScriptableObject when Unity starts. I have been using Resources.LoadAll and it DOES work, but I've noticed that everyone is advising against it on forums.
I've also tried FindObjectOfType<> as advised in this video but it doesn't work (or at least not before I click on it once in the Project window).
I'm really not sure what to do! :(
Answer by Esteem · Oct 22, 2018 at 03:47 PM
if you need this loading behaviour only for Editor purposes and you won't need it for the final build, you can create a custom editor for the scriptable object.
\\
[CustomEditor(typeof(YourVFXClass))]
public class YourVFXEditor : Editor {
public static YourVFXClass vfxDB;
static YourVFXCustomEditor() {
vfxDB = Resources.Load<YourVFXClass>("");
}
}
\\
And then you can reference it from anywhere (as long as it's from Editor/ folder script - which custom inspectors are) by calling YourVFXEditor.vfxDB
there are ofc other ways to do it, you can make a singleton and do it that way, it's up to you entirely.