(edtitor bug)? "static" scriptable object .asset compile warning

I ported the following static scriptableobject class from unity connect, for use in my game


public enum ObjType
{
    thing1,
    thing2
    //...and so on
}

[CreateAssetMenu]
public class ObjData: ScriptableObject
{
    [SerializeField]
    private GameObject[] objPrefabs_;
    [SerializeField]
    private Color[] objColors_;

    public GameObject ObjPrefabs(MissileType t)
    {
        return objPrefabs_[(int)t];
    }

    public Color ObjColors(MissileType t)
    {
        return objColors_[(int)t];
    }

    public static ObjData instance;
    const string path = "";

    [RuntimeInitializeOnLoadMethod]
    public static void Init()
    {
        instance = Resources.LoadAll<ObjData>(path)[0];
    }
}

(I dont want to get into a discussion about OOP, so please just trust that for my project and this context, a global reference to a list of prefabs and some related data is exactly what i want. In particular, I don’t want a monobehavior singleton)


This code compiles and runs fine. I am able to create an instance of this class and assign the desired fields in the editor without any issue. When my existing code from other classes access these methods, it works correctly.
However, if I do any of the following things: The asset file stops working completely.


  • compile code while in play mode
  • add new code which calls the methods in ObjData
  • modify the code in this file
  • close and open the project

The inspector says “the associated script cannot be loaded. please check for compilation errors and assign a valid script” when I select the asset. There aren’t any compile errors. If I manually copy-paste the .asset file, I get the same issue. If I create a new .asset file and copy paste the contents from the old .asset in a text editor, it doesn’t update, regardless of what I do with meta files. If move the .asset to a different folder, create a new .asset, fill out exactly the same data, and diff the two files, they are identical. The newly created asset file works until this problem occurs again.


This seems like an editor bug. There isn’t any warning or compilation errors at all, and identical files behave differently, even if i delete the .meta file to get it to reload. I even have tried only copying over the relevant entries in the yaml to get it to transfer over the data i want, but this doesn’t work either.


Is this an editor bug and how do I work around this issue?


Edit: if modify the code in a way that compiles, then undo the modification and recompile, the asset works again. I don’t want to constantly do this though…

If you want a SingletonScriptableObject, you can use this

public abstract class SingletonScriptableObject<T> : ScriptableObject where T : ScriptableObject
{
    private static T instance;
    public static T Instance
    {
        get
        {
            if (instance == null)
            {
                var objects = Resources.FindObjectsOfTypeAll<T>();
                foreach (var obj in objects)
                {
                    bool isAsset = UnityEditor.AssetDatabase.Contains(obj);
                    if (isAsset)
                    {
                        instance = obj;
                        break;
                    }
                }
                
                if(instance == null)
                {
                    //Create a temporary
                    instance = CreateInstance<T>();
                    instance.hideFlags = HideFlags.HideAndDontSave;
                    Debug.LogWarning($"SingletonScriptableObject of type {typeof(T).Name} could not be found");
                }
            }
            return instance;
        }
    }
}