- Home /
Other - I Solved it
URGENT - Cannot be serialized because it does not have a default public constructor....
Hi there,
I am trying to make a game and need a score system.
The error i get is: InvalidOperationException: ScoreData cannot be serialized because it does not have a default public constructor
And my code is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using UnityEngine.UI;
using System;
public class scoresSaver : MonoBehaviour {
public static ScoreList scorelist;
public GameObject HighScoreText;
public Transform parent;
public GameObject text;
public static List<ScoreData> scores = new List<ScoreData>();
void Start()
{
LoadScores();
}
public static void AddScore(string nameIn, int scoreIn)
{
XmlSerializer serializer = new XmlSerializer(typeof(ScoreList));
FileStream stream = new FileStream(Application.dataPath + "/StreamingAssets/highscores.xml", FileMode.Open);
scorelist = serializer.Deserialize(stream) as ScoreList;
foreach (ScoreData score in scorelist.scores)
{
scores.Add(score);
}
scores.Add(new ScoreData(nameIn, scoreIn));
}
public void LoadScores()
{
XmlSerializer serializer2 = new XmlSerializer(typeof(ScoreList));
FileStream stream2 = new FileStream(Application.dataPath + "/StreamingAssets/highscores.xml", FileMode.Open);
scorelist = serializer2.Deserialize(stream2) as ScoreList;
foreach (ScoreData score in scorelist.scores)
{
scores.Add(score);
}
scores.Sort();
scores.Reverse();
stream2.Close();
foreach (ScoreData score in scores)
{
print(score.Name);
print(score.Score);
text = Instantiate(HighScoreText);
text.transform.SetParent(parent);
text.GetComponent<Text>().text = score.Name + ": " + score.Score;
}
}
}
[System.Serializable]
public class ScoreData : IComparable<ScoreData>
{
public string Name;
public int Score;
public ScoreData(string newName, int newScore)
{
Name = newName;
Score = newScore;
}
public int CompareTo(ScoreData other)
{
if (this.Score > other.Score)
return 1;
else if (this.Score < other.Score)
return -1;
else
return 0;
}
}
[System.Serializable]
public class ScoreList
{
public List<ScoreData> scores = new List<ScoreData>();
}
I hope someone can help.
Many Thanks
Did you find a solution for this? Im having the same problem, and a parameterless constructor did not fix it.
That's not possible. This specific error is related to a missing parameterless constructor. So you either have a different error and not the one mentioned here, or you are still missing a public and parameterless constructor for your class. Note that classes without any explicit constructors do automatically get a parameterless constructor. However if you explicitly define a constructor with parameters, the implicit parameterless constructor will not be generated for you. This is different with structs. Structs always have a parameterless constructor and you can not remove it. This is because "default(SomeType)" will actually create an initialized instance of your struct. Initialized means the memory is filled with "0" / null. You also can not explicitly implement a parameterless constructor for structs as this is always implicit. Otherwise when creating an array of a struct type with 1000 elements, the constructor would need to run 1000 times which is not the case. The system just fills the memory with 0. That's also the reason why fields in structs can not have a field initializer. The default value of fields can not be changed, only with a constructor with parameters.
Answer by megabrobro · Dec 18, 2017 at 08:52 PM
You need to set a contructor that accepts no parameters, or if it has parameters they must have default values: https://www.google.co.uk/search?q=what+is+a+default+constructor&ie=utf-8&oe=utf-8&client=firefox-b-ab&gfe_rd=cr&dcr=0&ei=WSo4WsijBsiAgAaYnYX4DA
Follow this Question
Related Questions
Xml Serialization of "sub classes" 0 Answers
URGENT - Serializing error The type of argument is not primitive 0 Answers
Unity Serialization with XML Root problem: 'Does Not Denote Valid Type' 0 Answers
DataContract serialization not compatible with Unity? 0 Answers
[SOLVED] XML Deserialization of a single XML file into multiple objects 2 Answers