Scriptable object not loading at startup

I made an editor to make a database, based on these tutorials. All works fine but when I close and then reopen Unity it says “The associated Script can not be loaded. Please fix any compile errors and assign a valid script.”

My database (scriptable object) is in a separate folder in my assets.

This is in my editor class:

public partial class AbilityEditor : EditorWindow
{
    AbilityDatabase database;

    const string DATABASE_NAME = "AbilityDatabase.asset";
    const string DATABASE_PATH = "Databases";
    const string DATABASE_FULL_PATH = "Assets/" + DATABASE_PATH + "/" + DATABASE_NAME;

    [MenuItem("Game Systems/Databases/Ability Editor")]
    public static void Init()
    {
        AbilityEditor window = EditorWindow.GetWindow<AbilityEditor>();
        window.minSize = new Vector2(800, 600);
        window.titleContent = new GUIContent("Ability System");
        window.Show();
    }

    void OnEnable()
    {
        if (database == null)
            database = AbilityDatabase.GetDatabase<AbilityDatabase>(DATABASE_PATH, DATABASE_NAME);
    }

GetDatabase is this function. Note that this is in a different, generic class. it inherits from scriptableobject and then the class for the database inherits from this one:

    public static U GetDatabase<U>(string dbPath, string dbName) where U : ScriptableObject
    {
        string dbFullPath = "Assets/" + dbPath + "/" + dbName;

        U db = AssetDatabase.LoadAssetAtPath(dbFullPath, typeof(U)) as U;

        Debug.LogWarning("Running this function");
        Debug.Log("db: " + db);

        if (db == null)
        {
            Debug.LogWarning("DATABASE IS NULL, MAKING A NEW ONE");
            
            //Check to see if the folder exists
            if (!AssetDatabase.IsValidFolder("Assets/" + dbPath))
                AssetDatabase.CreateFolder("Assets", dbPath);

            //Create the database and refresh the AssetDatabase
            db = ScriptableObject.CreateInstance<U>() as U;
            AssetDatabase.CreateAsset(db, dbFullPath);
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
        }

        return db;
    }

The scriptableObject is always null, but only after start up. When I reopen the editor it does find it and does not make a new one (which erases the existing one if done).

The thing is, the scriptableObject is always having a “missing script” error in the editor, right after I just launch Unity. But opening once, and stop, will make the scriptableObjects seem to “remember” again the values in it. I really don’t know what’s causing this.

I just recently had this exact problem and found that the missing script field was requesting a reference to the data object (class/struct) in the assets folder. I simply created a script that defined the class I was trying to instantiate, the editor script found it and loaded it into the correct field.

I’m not sure if this will work in your case, but try making a database script that defines the database class and saving it in your asset database. So essentially you would have a script called database.cs with the class definition:

public class database: ScriptableObject
{
//object definition
}