ERROR when trying to serialize a list

Updated on January 20, 2017 in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
0 on January 20, 2017

I have a Player class which hold information about a player, I also have a existingPlayers class which holds a list of all the existing players.

I have it when a user clicks a button after putting his name and choosing a color it will create a new player and add it to a list and then serializing that list into XML.

Player class


public class Player

{

public string name { get; set; }

public string color { get; set; }

public int wins { get; set; }

public int losses { get; set; }

}

ExistingPlayers class:


using UnityEngine;

using System.Collections.Generic;

using System.Xml.Serialization;
public class ExistingPlayers : MonoBehaviour

{

[XmlArray("Tasks")]

public List<Player> existingPlayers;
public void CreateList()

{

existingPlayers = new List<Player>();

}

}

Creating a new player:


using UnityEngine;

using UnityEngine.UI;

using System.IO;

using System.Collections.Generic;

using System.Xml.Serialization;
public class NewPlayer : MonoBehaviour

{

public ExistingPlayers existingPlayers;
XmlSerializer xmlS;

StreamWriter str;
public InputField playerNameIF;

public ColorPicker picker;
public Color startingColor;
void Start()

{

picker.CurrentColor = startingColor;

}
public void CreateNewPlayer()

{

Player newPlayer = new Player();

newPlayer.name = playerNameIF.text;

newPlayer.color = picker.CurrentColor.ToString();

newPlayer.wins = 0;

newPlayer.losses = 0;
if(existingPlayers.existingPlayers == null)

{

existingPlayers.existingPlayers = new List<Player>();

}
if(xmlS == null)

{

xmlS = new XmlSerializer(existingPlayers.GetType());

}
if(str == null)

{

str = new StreamWriter(@Application.persistentDataPath + "/playerData.xml");

}
Debug.Log(Application.persistentDataPath + "/playerData.xml");
existingPlayers.existingPlayers.Add(newPlayer);
xmlS.Serialize(str.BaseStream, existingPlayers.existingPlayers);

}

}

But the problem is I get an error saying:


InvalidOperationException: To be XML serializable, types which inherit from IEnumerable must have an implementation of Add(System.Object) at all levels of their inheritance hierarchy. UnityEngine.Transform does not implement Add(System.Object).

System.Xml.Serialization.TypeData.get_ListItemType ()

System.Xml.Serialization.TypeData.get_ListItemTypeData ()

System.Xml.Serialization.TypeData..ctor (System.Type type, System.String elementName, Boolean isPrimitive, System.Xml.Serialization.TypeData mappedType, System.Xml.Schema.XmlSchemaPatternFacet facet)

System.Xml.Serialization.TypeData..ctor (System.Type type, System.String elementName, Boolean isPrimitive)

System.Xml.Serialization.TypeTranslator.GetTypeData (System.Type runtimeType, System.String xmlDataType)

System.Xml.Serialization.TypeTranslator.GetTypeData (System.Type type)

System.Xml.Serialization.XmlReflectionImporter.GetReflectionMembers (System.Type type)

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 'ExistingPlayers'.

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)

NewPlayer.CreateNewPlayer () (at Assets/Scripts/NewPlayer.cs:39)

UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:153)

UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:634)

UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:769)

UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:53)

UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35)

UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44)

UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:52)

UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:269)

UnityEngine.EventSystems.EventSystem:Update()

I googled a bit and it said that I have a MonoBehaviour on one of my classes, I thing it’s the existing players class, but if I remove the MonoBehaviour I can’t access the script in the inspector (since I need to assign the script on the NewPlayer class to access the list).

  • Liked by
Reply