- Home /
object list not serializing
i have an editor window that modifies variable of scene gameobject. the variable was List and it was working fine, but then i changed modified the code and i needed it to be List. and now it's not serializing (it's cleared when entering play mode).
//Before
[SerializeField]
public List<GameObject> returnValues;
//After
[SerializeField]
public List<object> returnValues;
Answer by frarees · Mar 25, 2014 at 04:44 PM
You can't serialize object
. Make a List of a serializable object type instead e.g. Vector3, int, float, ... check this.
If you want to hold custom classes e.g. MyData
make sure you mark the class as Serializable
i.e.:
[System.Serializable]
public class MyData {
public int intValue;
public UnityEngine.Object objectValue;
public float floatValue;
public string stringValue;
...
}
[SerializeField]
List<MyData> returnValues;
but i need a list that will hold different values (string, int or GameObject in my case). why object can't be serialized?
Unity won't be able to render object
to disk, there's no serialized representation for it.
You can't hold different values there unless all of them are inherit from a common serializable class e.g. UnityEngine.Object
, where you can hold Texture2D
, GameObject
, Component
...
Also, worth mentioning polymorphic serialization (not officially supported by Unity yet AFAI$$anonymous$$, but you can make it work)
I've changed the code snippet so that $$anonymous$$yData
can hold the values you want to store. You just need to know which type is each $$anonymous$$yData
instance to retrieve it property (as SerializedProperty
does, for example)
Answer by gamesappstudio · Jul 11, 2017 at 06:58 PM
You can find plugin here (easy to implement ) http:// assetstore.unity3d.com/en/#!/content/94493
Your answer
Follow this Question
Related Questions
Got a Problem when serializing Class Dictionary with Custom Editor 1 Answer
Don't save gameobjects to scene file (No hide flags) 0 Answers
Save Data from Editor Script, Serialize Nested Class List 1 Answer
SerializedProperty with children class fields 2 Answers
Delete a serialized binary save file 2 Answers