Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Yet another problem with unity's serialization

Discussion in 'Scripting' started by geroppo, Nov 13, 2014.

  1. geroppo

    geroppo

    Joined:
    Dec 24, 2012
    Posts:
    140
    So here's the thing:

    Code (CSharp):
    1. [System.Serializable]
    2. public class SomeItem
    3. {
    4.    public string someString;
    5. }
    6. public class Container : ScriptableObject
    7. {
    8.       [SerializeField]
    9.      List<Someitem> items;
    10.        public void addItem()
    11.        {
    12.             items.Add(new SomeItem());
    13.        }
    14. }
    If i populate the list using the scriptableobject's inspector, everything is saved correctly. But if i use the method addItem, the new item doesn't survive when i press play and i can't quite find the problem, any ideas ? ( the method is being called from an editor window btw )
     
  2. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    I know that when trying to serialize a list population from an editor script within a prefab that you must apply the prefab afterwards. Maybe you could try to, File->Save project as ScriptableObjects are saved as assets.
     
  3. geroppo

    geroppo

    Joined:
    Dec 24, 2012
    Posts:
    140
    well...that worked lol thanks, but there must be a better way than doing it manually ( and for some reason AssetDatabase.SaveAssets() doesn't work :( )
     
    Last edited: Nov 13, 2014
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
    in the editor you have a reference to the 'SerializedObject' for the class 'Container'.

    When you add to the list, you have to then call 'serializedObject.Update()'. This tells the system to update the serializedObject with the state of the object how it currently is in memory.

    Similarly serializedObject.ApplyModifiedProperties tells the system to update the object with the state of the serializedObject.
     
  5. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Maybe try, EditorApplication.SaveAssets() ?
     
  6. geroppo

    geroppo

    Joined:
    Dec 24, 2012
    Posts:
    140
    ah i was just using a simple object reference to the class so i could call the methodd addItem ( in the window class i have a variable of type container, and i initialize that variable using the resource.load), so you say that i create an instance of serializedObject so that i can call the update and apply methods? or you say that i handle everything with serialized object and properties?
     
  7. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
    If it's a custom Editor that handles the type... you already have the serializedObject reference.

    If it's not a custom editor, and you're creating an instance and modifying it in some custom window. Then you don't have the serializedObject to work with. Here, yes, you have to call AssetDatabase.SaveAssets()... BUT before you call that you have to set the object dirty.

    Code (csharp):
    1.  
    2. //getting object
    3. var c = *reference to the Container*;
    4.  
    5. //modifying object
    6. c.addItem(...);
    7.  
    8. //if modified
    9. EditorUtility.SetDirty(c);
    10. AssetDatabase.SaveAssets();
    11.  
    Note, AssetDatabase.SaveAssets saves all assets that have been SetDirty. If you haven't set anything as dirty, there is nothing for SaveAssets to save.
     
  8. geroppo

    geroppo

    Joined:
    Dec 24, 2012
    Posts:
    140
    oh i see, didnt knew about the set dirty one, thanks alot