- Home /
ScriptableObject loses data after Reload
I have made database of the Enemies class, then I have a EditorWindow where I add and edit my Database, but whenever I close and open my Unity, all data is gone! But when I play and stop my game, the data is still there tho. I have serialized all classes that I needed, made and saved my ScriptableObject as an Asset, but still all my data is lost after Unity is closed.
[System.Serializable]
public class EnemyDatabase : ScriptableObject {
[SerializeField] public List<Enemy> database;
void OnEnable() {
if (database == null) {
database = new List<Enemy> ();
}
}
}
Here is the Enemy class:
[System.Serializable]
public class Enemy {
[SerializeField] public string enemyName;
[SerializeField] public int id;
[SerializeField] public int damage;
[SerializeField] public int points;
[SerializeField] public float speed;
[SerializeField] public float chance;
[SerializeField] public GameObject prefab;
public Enemy(string name, int newId, int dmg, int pnts, float vel, float rarity, GameObject go) {
enemyName = name;
id = newId;
damage = dmg;
points = pnts;
speed = vel;
chance = rarity;
prefab = go;
}
}
And the EditorWindow script, where I create a new Database if there isn't one, and then save it as an Asset:
void CreateDatabase() {
enemyDatabase = ScriptableObject.CreateInstance<EnemyDatabase> ();
AssetDatabase.CreateAsset (enemyDatabase, DATABASE_PATH);
AssetDatabase.SaveAssets ();
AssetDatabase.Refresh ();
}
Answer by x4637x · Dec 30, 2017 at 11:08 AM
From your "EditorWindow " script you are only creating new "enemyDatabase" and not loading the existing one?
Have you exposed your "enemyDatabase" variable in your inspector? If you have, you could drag and drop your saved asset to the field and it will be loaded.
I am loading the existing one. $$anonymous$$y enemyDatabase is a private field and not serialized.
You could debug this by setting up EnemyDatabase.name
for your saved asset. And have the console print out its name everytime you trying to use it. So you could find out from where it starts to get lost.
And the name attribute should be inherited from UnityEngine.Object
class so you don't need to create your own.
Your answer
Follow this Question
Related Questions
List of ScriptableObjects lost on project reload 1 Answer
Saving Game Confussion 1 Answer
[Solved]How to serialize Dictionary with Unity Serialization System 6 Answers
Saving Non-Prefab Data During Editor Play 0 Answers
Difference between assigning a value in inspector and with a custom editor script 1 Answer