- Home /
Question by
Darkhalo45 · Feb 01, 2018 at 07:20 PM ·
serializationscriptableobjecteditorwindow
ScriptableObject does not serialize
For some reason, my ScriptableObject does not save the list I have on it. I am trying to make a database. I tried everything to fix it, even copied the code from my older project(SO work properly there), but still nothing happens. This is the ScriptableObject script:
public class TestEnemyDatabase : ScriptableObject {
[SerializeField]
List<TestEnemyScript> database = new List<TestEnemyScript>();
public void Add(TestEnemyScript enemy) {
database.Add (enemy);
}
}
Item class:
[System.Serializable]
public class TestEnemyScript {
[SerializeField]
int _damage;
[SerializeField]
float _health;
public TestEnemyScript(int dmg, float hp) {
_damage = dmg;
_health = hp;
}
public int Damage {
get { return _damage; }
set { _damage = value; }
}
public float Health {
get { return _health; }
set { _health = value; }
}
}
And the Editor Script:
public class TestDatabaseEditor : EditorWindow {
int newDamage;
float newHealth;
private TestEnemyDatabase enemyDatabase;
private const string DATABASE_PATH = "Assets/TestDatabase.asset";
[MenuItem("Window/Test Enemy Database")]
public static void ShowWindow() {
EditorWindow window = EditorWindow.GetWindow (typeof(TestDatabaseEditor));
window.Show ();
}
void OnEnable() {
if (enemyDatabase == null) {
LoadDatabase ();
}
}
void LoadDatabase() {
enemyDatabase = (TestEnemyDatabase)AssetDatabase.LoadAssetAtPath (DATABASE_PATH, typeof(TestEnemyDatabase));
if (enemyDatabase == null) {
CreateDatabase ();
}
}
void CreateDatabase() {
// Debug.Log ("New Enemy Database has been created.");
enemyDatabase = ScriptableObject.CreateInstance<TestEnemyDatabase> ();
AssetDatabase.CreateAsset (enemyDatabase, DATABASE_PATH);
AssetDatabase.SaveAssets ();
AssetDatabase.Refresh ();
}
void OnGUI() {
newDamage = EditorGUILayout.IntField ("Damage: ", newDamage);
newHealth = EditorGUILayout.FloatField ("Health: ", newHealth);
if (GUILayout.Button("Add a new item")) {
// TestEnemyScript enemyScript = new TestEnemyScript (newDamage, newHealth);
enemyDatabase.Add (new TestEnemyScript (newDamage, newHealth));
}
}
}
Comment
On the editor script after line 46 add EditorUtility.SetDirty(enemyDatabase);
It works! Thak you, but wasn't SetDirty() obsolete before??
Well, there is one inside another class, i think Editor.SetDirty() , not sure though, which is obsolete.
maybe try using public variables inse$$anonymous$$d of serialized fields they are automatically serialized
Your answer
![](https://koobas.hobune.stream/wayback/20220612151742im_/https://answers.unity.com/themes/thub/images/avi.jpg)