- Home /
Why does my scriptable object asset lose items in a list after first playthrough?
Hello everyone! Long time no see in these forums. Im currently using UnityEngine 2019.3.9f1
So essentially I wanted to create a super versatile dialog system (with textmesh pro). I made an editor window and everything. It essentially works like a nintendo game from the early 2000s - it feeds text through the UI by individual dialog boxes (or one instance of blurb before waiting for the input for next chat box). But it also feeds by individual words too, meaning you can apply effects to words that are important, slow down the type speed of individual words to show a character is thinking, or make font bigger to show a character is angry. This tool also triggers events when the dialog is closed. I do intend on making this tool public and free use when I feel its full :)
Heres the thing, its done and functional (even has some effects). I can create a "dialog asset" just fine. Then I can plug it in perfectly the first playthrough, but heres the problem, when I close out of the first test, all the child objects inside the dialog asset are missing (I'll explain how it works later). Before I do the first playthrough however, the list of "snipets" when opened are labeled as "type mismatch"
All the dialog assets are made up of is three parts: The Dialog, the snipets, and the lines. All three have their own respective public scriptable object classes.
When you open the editor window it creates a new instance of dialog. All that is inside Dialog is a public list of Snipets. When you press the large button that says create new snippet, using the CreateInstance(typeof(Snipet)) function, it adds a snippet to the list of snippets. Inside of snippets, theres some floats and bools but the main thing is a list of type line. When you press the Create new line, it does the same thing as before with the snipets.
When you press save dialog, I use AssetDatabase.CreateAsset and it makes everything just fine. I can even press the "type mismatch" and it opens the given snipet in the inspector. This is my first time really working with scriptable objects and making my own tool, is there something else Im supposed to be doing? Like I said before everything works the first playthrough but after Im done, it wipes the dialog asset's snipet children. Id supply screenshots but I keep getting an error right now.
Answer by Bunny83 · Jun 24, 2020 at 01:49 AM
Did you actually save any of your child objects? So all those snippets and line objects? ScriptableObjects are standalone assets. They either have to be stored in a seperate .asset file or you could pack multiple objects into the same asset file by using AssetDatabase.AddObjectToAsset.
References to scriptable objects are actual serialized references. Just adding scriptable objects into a serialized list won't save all those objects. When you create a ScriptableObject with CreateInstance it only exist in memory / in the current scene. If you want such an object to persist you have to use CreateAsset or AddObjectToAsset.
Thank you very much! I had no idea about AddObjectToAsset! Would I have to add each line after I create the initial asset? (I can simply loop through them, I just need to know when I should do AddObject to Asset). Very helpful information though, I just started getting into all the obscure serialize stuff.
Your answer
Follow this Question
Related Questions
Object field not working in custom editor for scriptable object? 0 Answers
[Custom Editor] MonoBehaviour vs Scriptable Object 0 Answers
"Unbroken Reference" problem when using a custom Editor to Save/Load a ScriptableObject Asset 0 Answers
Creating a Scriptable Object Custom Editor Footer 0 Answers
ScriptableObject Custom Asset Error on Build and First Starting Unity 0 Answers