- Home /
Question by
Eleyox · Mar 29, 2018 at 08:13 PM ·
listserializationxml
[SAVING] LIST cannot be serialized because it does not have a default public constructor
Hi. I tried to follow some tutorials about saving and creating list.
Imagine a Cookie Clicker with buyable buildings, that's what I wanted.
So I created a list with a string (Name of the building) and an int (Amount of buildings)
public class GoatList : IComparable<GoatList>
{
string name;
int owned;
public GoatList(string newName, int newOwned)
{
name = newName;
owned = newOwned;
}
public int CompareTo(GoatList other)
{
if (other == null)
{
return 1;
}
//Return the difference in power.
return owned - other.owned;
}
}
(The code is not finished, created with the Unity Tutorial)
And then I wanted to save this to the player's savestate :
public List<savemanager.GoatList> goat;
The problem is that I'm having this error :
InvalidOperationException: savemanager+GoatList cannot be serialized because it does not have a default public constructor
System.Xml.Serialization.ReflectionHelper.CheckSerializableType (System.Type type, Boolean allowPrivateConstructors)
System.Xml.Serialization.XmlReflectionImporter.ImportClassMapping (System.Xml.Serialization.TypeData typeData, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace)
System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping (System.Xml.Serialization.TypeData typeData, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace)
Rethrow as InvalidOperationException: There was an error reflecting type 'savemanager+GoatList'.
System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping (System.Xml.Serialization.TypeData typeData, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace)
System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping (System.Type type, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace)
System.Xml.Serialization.XmlReflectionImporter.ImportListMapping (System.Xml.Serialization.TypeData typeData, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace, System.Xml.Serialization.XmlAttributes atts, Int32 nestingLevel)
System.Xml.Serialization.XmlReflectionImporter.ImportListMapping (System.Type type, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace, System.Xml.Serialization.XmlAttributes atts, Int32 nestingLevel)
System.Xml.Serialization.XmlReflectionImporter.CreateMapMember (System.Type declaringType, System.Xml.Serialization.XmlReflectionMember rmember, System.String defaultNamespace)
System.Xml.Serialization.XmlReflectionImporter.ImportClassMapping (System.Xml.Serialization.TypeData typeData, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace)
Rethrow as InvalidOperationException: There was an error reflecting field 'goat'.
System.Xml.Serialization.XmlReflectionImporter.ImportClassMapping (System.Xml.Serialization.TypeData typeData, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace)
System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping (System.Xml.Serialization.TypeData typeData, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace)
Rethrow as InvalidOperationException: There was an error reflecting type 'savestate'.
System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping (System.Xml.Serialization.TypeData typeData, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace)
System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping (System.Type type, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace)
System.Xml.Serialization.XmlSerializer..ctor (System.Type type, System.Xml.Serialization.XmlAttributeOverrides overrides, System.Type[] extraTypes, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace)
System.Xml.Serialization.XmlSerializer..ctor (System.Type type)
helper_script.Deserialize[savestate] (System.String toDeserialize) (at Assets/Scripts/helper_script.cs:18)
savemanager.Load () (at Assets/Scripts/savemanager.cs:29)
savemanager.Awake () (at Assets/Scripts/savemanager.cs:15)
How can I fix this ?
Sorry for the messy code, I'm still learning.
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612155627im_/https://answers.unity.com/themes/thub/images/avi.jpg)