So i'm writing spell system with using some kind of spell component system. Spells itself and spell components are Scriptable Objects. Spell to create as asset and SpellComponent because non unity objects can't be properly serialized with polymorphyc classes. So when i edit freshly created spell and enter play mode they remain as i edited them. But the problem is after second assembly reload my scriptable object loses all of it's data.
I thought that it might be garbage collected. But i used hideFlags=DontUnloadUnusedAsset and it didn't worked. Then i tried using hideFlags HideAndDontSave referring to this article . It didn't work and i couldn't edit my variables. Here's some code for example.
[CreateAssetMenu(menuName = "Spell", fileName = "New Spell")]
[System.Serializable]
public class Spell : ScriptableObject
{
[SerializeField] private List<SpellComponent> m_Components;
public virutal void OnEnable()
{
hideFlags = HideFlags.DontUnloadUnusedAsset;
}
}
[System.Serializable]
public abstract class SpellComponent : ScriptableObject
{
[SerializeField] int m_ExampleInt;
public virtual void OnEnable()
{
hideFlags = HideFlags.DontUnloadUnusedAsset;
}
}
So to recreate the problem.
- Create instance of Spell with AssetMenu
- Add SpellComponent to Spell
- Edit m_ExampleInt with Inspector
- Enter playmode (m_ExampleInt will not change)
- Enter playmode again (m_ExampleInt will be 0)