1

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

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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");

1 Comment

AssetDatabase only exists in UnityEditor, which can't be used at runtime.
0

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");
}

1 Comment

AssetDatabase only exists in UnityEditor, which can't be used at runtime.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.