Search Unity

Scriptable with List of Scriptables

Discussion in 'Scripting' started by FreeStaila, Oct 8, 2019.

  1. FreeStaila

    FreeStaila

    Joined:
    Apr 26, 2017
    Posts:
    1
    Hello,

    I have a little levels generator. Script creating scriptable (levels list) with list filled with scriptables (levels). Each level have also a list with scriptables. Almost everything works pretty well but after Unity restart, level scriptable is empty.

    This is a level class:
    Code (CSharp):
    1.  
    2. public class LevelPanelsList : ScriptableObject
    3. {
    4.     public List<LevelPanel> lvlPanelsList = new List<LevelPanel>();
    5. }
    6.  
    7. I using this line to save my assets when i creating them:
    8.  
    Code (CSharp):
    1.  
    2. AssetDatabase.SaveAssets();
    When I put everything manualy to the list, click ctrl+s and restart Unity it working but it's not the way i would like to do this.
    What I doing wrong?
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,282
    Are you saving the Instances to assets? You need to save them to asset files, each one needs to be an asset for them to persist.
    To create an asset use:
    https://docs.unity3d.com/ScriptReference/AssetDatabase.CreateAsset.html

    To append an object onto an existing asset you can also use
    https://docs.unity3d.com/ScriptReference/AssetDatabase.AddObjectToAsset.html


    If the problem is that you are making changes in script and they are not saving then it could be because Unity does not know you have made changes.
    you can use 1 of the following to make a change:
    https://docs.unity3d.com/ScriptReference/SerializedObject.html
    https://docs.unity3d.com/ScriptReference/Undo.html
    https://docs.unity3d.com/ScriptReference/EditorUtility.SetDirty.html