- Home /
URGENT - Serializing error The type of argument is not primitive
Hi there,
I really need some help!
This is the error i am getting: InvalidOperationException: The type of the argument object 'System.Collections.Generic.List`1[[ScoreData, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]]' is not primitive.
And here is my code:
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)
{
scorelist.scores.Add(new ScoreData { Name = nameIn, Score = scoreIn });
var serializer = new XmlSerializer(typeof(ScoreList));
var stream = new FileStream(Application.dataPath + "/StreamingAssets/highscores.xml", FileMode.Open);
serializer.Serialize(stream, scorelist.scores);
stream.Close();
}
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);
print(score);
}
scores.Sort();
scores.Reverse();
stream2.Close();
stream2.Dispose();
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 { set; get; }
public int Score { set; get; }
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>();
}
The error is at the line: serializer.Serialize(stream, scorelist.scores);
I hope someone can help
Many Thanks
Comment