I’ve been working on an editor tool for my game that allows the creation/modification of items. In my editor window script, when the user wants to save the current state of the item manager, I create an asset composed of the item manager (which derives from ScriptableObject) and save in the Resources folder for later use. However, if I try to make a change and re-save it, if I don’t call AssetDatabase.DeleteAsset() it tells me that the asset already exists. If I do call AssetDatabase.DeleteAsset(), then when I subsequently call AssetDatabase.CreateAsset() it tells me that the object is null. Also, upon trying to load the asset from file in the editor with AssetDatabase.GetAssetAtPath() or in game with Resources.Load(), the ItemManager variable comes up null.
Here’s some supplemental code that will hopefully help you understand what is going on.
So after taking a break, and then coming back to it for a couple hours I realized that some of my classes were not marked [Serializable], and I should’ve been Saving/Loading the file like this:
//This is called when the Editor Window is created
itemmanager = (ItemManager)AssetDatabase.LoadAssetAtPath(@"Assets\Resources\ItemManager.asset", typeof(ItemManager));
if(itemmanager == null)
{
AssetDatabase.CreateAsset(itemmanager, @"Assets\Resources\ItemManager.asset");
itemmanager = (ItemManager)ScriptableObject.CreateInstance(typeof(ItemManager));
}
//And this is called when I want to save
if(!AssetDatabase.Contains(itemmanager))
AssetDatabase.CreateAsset(itemmanager, @"Assets\Resources\ItemManager.asset");
AssetDatabase.SaveAssets();
Also i found that your scriptableobject script must be defined in separated class,
Otherwise, when you use AssetDatabase.Create, it’s create asset well, but lost links to the script.
Yes… to fix the “lost link to script” issue, you should make a separated cs file such as MyScriptableObject.cs that contains a class named MyScriptableObjec.
//in MyScriptableObject.cs
public class MyScriptableObject: ScriptableObject
{
... ...
}