Search Unity

Data disappearing on ScriptableObject

Discussion in 'Scripting' started by Rob-Howard, Aug 30, 2015.

  1. Rob-Howard

    Rob-Howard

    Joined:
    Sep 4, 2014
    Posts:
    9
    So I've had several problems getting this damn item database to work properly. Here is a rundown of what I'm doing:

    - A scriptable object that holds three lists for crafting materials in my game. All three lists are tagged as SerializedField
    - The materials class is marked serializable, along with all fields as serialized fields
    - The custom editor calls EditorUtility.SetDirty on the field containing the asset when changes are made to the lists (adding/deleting items)

    However, I'm having problems with the items inside the lists of the scriptable objects. However, when I exit unity and restart, the data is not saved. The lists still show objects inside them, but the details of those objects no longer exists. Also, I'm getting a null reference exception when I close and open unity and then try to open the .asset file in my custom window.

    Here is the code and screenshots:

    ScriptableObject
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class ItemDatabase : ScriptableObject
    6. {
    7.     [SerializeField]
    8.     public List<Material> baseMaterials;
    9.     [SerializeField]
    10.     public List<Material> specialMaterials;
    11.     [SerializeField]
    12.     public List<Material> adventureMaterials;
    13.  
    14.     void OnEnable()
    15.     {
    16.         if(baseMaterials == null)
    17.         {
    18.             Debug.Log("Base materials is null");
    19.             baseMaterials = new List<Material>();
    20.         }
    21.  
    22.         if(specialMaterials == null)
    23.         {
    24.             specialMaterials = new List<Material>();
    25.         }
    26.  
    27.         if(adventureMaterials == null)
    28.         {
    29.             adventureMaterials = new List<Material>();
    30.         }
    31.     }
    32. }
    The save function inside the Custom Window:
    Code (CSharp):
    1.     void SaveItem(bool baseItem, bool specialItem, bool adventureItem, Material newItem)
    2.     {
    3.  
    4.         if (baseItem)
    5.             itemDatabase.baseMaterials.Add(newItem);
    6.         if (specialItem)
    7.             itemDatabase.specialMaterials.Add(newItem);
    8.         if (adventureItem)
    9.             itemDatabase.adventureMaterials.Add(newItem);
    10.  
    11.         EditorUtility.SetDirty(itemDatabase);
    12.         AssetDatabase.SaveAssets();
    13.         AssetDatabase.Refresh();
    14.     }
    The Material class:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [System.Serializable]
    5. public class Material : ICraftingMaterial
    6. {
    7.     [SerializeField]
    8.     public string Name { get; set; }                        //Name of Material
    9.     [SerializeField]
    10.     public string Description { get; set; }                 //Description of Material
    11.     [SerializeField]
    12.     public int Value { get; set; }                          //Value of material in Gold
    13.     [SerializeField]
    14.     public Sprite Icon { get; set; }                        //Sprite ICON Image of Material
    15.     [SerializeField]
    16.     public int ItemID { get; set; }                         //Unique ID of Material
    17.     [SerializeField]
    18.     public int BaseD { get; set; }                          //Base Damage or Defense of Material
    19.     [SerializeField]
    20.     public MaterialType MatType { get; set; }               //Type of material (Base, Special, Adventure)
    21.     [SerializeField]
    22.     public MaterialElement ModType { get; set; }            //Elemental Modification Type of material
    Here is a screenshot showing the data properly inside:

    Here is a screenshot after restart:


    Any help would be greatly appreciated.
     
  2. Rob-Howard

    Rob-Howard

    Joined:
    Sep 4, 2014
    Posts:
    9
    I've done some more testing and I've found out a few...odd things:

    If I save the project/scene and then hit play/stop, the data is destroyed for some reason. (Just like closing and reopening unity)

    However, if I DON'T save the project or the scene, hit play/stop, the data remains in tact. The data is only destroyed when you save the project/scene. I don't know why that is, but it's damn strange.
     
  3. Rob-Howard

    Rob-Howard

    Joined:
    Sep 4, 2014
    Posts:
    9
    DUR. I figured it out.

    I was attempting to serialize properties instead of the actual fields. Once I added actual backing fields to the properties and marked those as serializable, everything was fixed.