Data disappearing on ScriptableObject

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

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ItemDatabase : ScriptableObject
{
    [SerializeField]
    public List<Material> baseMaterials;
    [SerializeField]
    public List<Material> specialMaterials;
    [SerializeField]
    public List<Material> adventureMaterials;

    void OnEnable()
    {
        if(baseMaterials == null)
        {
            Debug.Log("Base materials is null");
            baseMaterials = new List<Material>();
        }

        if(specialMaterials == null)
        {
            specialMaterials = new List<Material>();
        }

        if(adventureMaterials == null)
        {
            adventureMaterials = new List<Material>();
        }
    }
}

The save function inside the Custom Window:

    void SaveItem(bool baseItem, bool specialItem, bool adventureItem, Material newItem)
    {

        if (baseItem)
            itemDatabase.baseMaterials.Add(newItem);
        if (specialItem)
            itemDatabase.specialMaterials.Add(newItem);
        if (adventureItem)
            itemDatabase.adventureMaterials.Add(newItem);

        EditorUtility.SetDirty(itemDatabase);
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
    }

The Material class:

using UnityEngine;
using System.Collections;

[System.Serializable]
public class Material : ICraftingMaterial
{
    [SerializeField]
    public string Name { get; set; }                        //Name of Material
    [SerializeField]
    public string Description { get; set; }                 //Description of Material
    [SerializeField]
    public int Value { get; set; }                          //Value of material in Gold
    [SerializeField]
    public Sprite Icon { get; set; }                        //Sprite ICON Image of Material
    [SerializeField]
    public int ItemID { get; set; }                         //Unique ID of Material
    [SerializeField]
    public int BaseD { get; set; }                          //Base Damage or Defense of Material
    [SerializeField]
    public MaterialType MatType { get; set; }               //Type of material (Base, Special, Adventure)
    [SerializeField]
    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.

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.

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.