StackBuddy
StackBuddy

Reputation: 577

Creating a scriptable object in runtime - It creates instances but doesn't save them in asset folder?

enter image description here So I have a little question I'm creating scriptable objects first time from spreadsheet data trying to use the database to automagically make them...

for (int m = 0; m < myAssets.Count; m++)
            {
               Drill_WordBase newWordAsset = ScriptableObject.CreateInstance<Drill_WordBase>();

                // overwrite the defaults with the json data
                newWordAsset.word = word;
                    newWordAsset.isTrue = false;
                    myList.Add(newWordAsset);

                AssetDatabase.CreateAsset(newWordAsset, "Assets/DrillWords/" + newWordAsset.name);
                AssetDatabase.SaveAssets();
                AssetDatabase.Refresh();
                EditorUtility.FocusProjectWindow();
                Selection.activeObject = newWordAsset;
            }

it "partly works" however I don't understand how to get it to save the files properly. Since in the Assets/Drill_Words/ folder there is some kind of file there but not a scriptable object with all of the info (see pic) However, the DrillWords list has scriptable objects, I haven't yet tested if they have all the data, I lose these scriptable objects after stopping playing?

Any idea what I am doing wrong, how to I save them?

With thanks - N

Upvotes: 1

Views: 10739

Answers (3)

user507095
user507095

Reputation: 31

As per the latter, this works and operates during runtime.

void Start()
{
    createScriptableObject();
}

void createScriptableObject()
{
    CustomClass newItem = ScriptableObject.CreateInstance<CustomClass >();
    newItem.name = "name";
    AssetDatabase.CreateAsset(newItem, "Assets/NewScriptableClasses/Test.asset");
}

Upvotes: 0

ProtoTyPus
ProtoTyPus

Reputation: 1324

I don't know if you have solved it.

However, you were doing right, you only missed this:

AssetDatabase.CreateAsset(newWordAsset, "Assets/DrillWords/" + newWordAsset.name + ".asset");

Upvotes: 2

DoctorPangloss
DoctorPangloss

Reputation: 3073

You cannot save scriptable objects during runtime. Use an editor script instead.

Otherwise, if you are loading a "spreadsheet" in runtime as part of the application/game, use JSONUtility instead to serialize and deserialize common [Serializable] classes and structs with [SerializeField] instead.

Upvotes: 2

Related Questions