- Home /
xmlserializer Serializing/deserializing error
Edit: I managed to pinpoint the issue. Whenever MusicPart is serialized, when deserializing it throws the error below.
Here is my MusicPart class:
[System.Serializable]
public class InstrumentPart
{
public AudioClip[] clips;
public int initialTrack = -1;
}
[System.Serializable]
public class MusicPart
{
public InstrumentPart mainClips, bassClips, chordsClips, melodyClips, drumClips, percClips;
private double nextSwitch;
}
And the XML file:
<?xml version="1.0" encoding="us-ascii"?>
<Checkpoint xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<nextMarker>Goal - Challenge 2</nextMarker>
<difficulty>1</difficulty>
<music>
<mainClips>
<clips>
<AudioClip>
<name>br_m_s0p0_main</name>
<hideFlags>NotEditable</hideFlags>
</AudioClip>
</clips>
<initialTrack>0</initialTrack>
</mainClips>
<bassClips>
<clips>
<AudioClip>
<name>br_m_s0p0_bass_acoalt</name>
<hideFlags>NotEditable</hideFlags>
</AudioClip>
<AudioClip>
<name>br_m_s0p0_bass_aco</name>
<hideFlags>NotEditable</hideFlags>
</AudioClip>
<AudioClip>
<name>br_m_s0p0_bass_dist</name>
<hideFlags>NotEditable</hideFlags>
</AudioClip>
<AudioClip>
<name>br_m_silence</name>
<hideFlags>NotEditable</hideFlags>
</AudioClip>
</clips>
<initialTrack>1</initialTrack>
</bassClips>
<chordsClips>
<clips>
<AudioClip>
<name>br_m_s0p0_chords_strings</name>
<hideFlags>NotEditable</hideFlags>
</AudioClip>
<AudioClip>
<name>br_m_s0p0_chords_choir</name>
<hideFlags>NotEditable</hideFlags>
</AudioClip>
<AudioClip>
<name>br_m_s0p0_chords_horns</name>
<hideFlags>NotEditable</hideFlags>
</AudioClip>
<AudioClip>
<name>br_m_silence</name>
<hideFlags>NotEditable</hideFlags>
</AudioClip>
</clips>
<initialTrack>0</initialTrack>
</chordsClips>
<melodyClips>
<clips>
<AudioClip>
<name>br_m_s0p0_melodi_trumpet</name>
<hideFlags>NotEditable</hideFlags>
</AudioClip>
<AudioClip>
<name>br_m_s0p0_melodi_piano</name>
<hideFlags>NotEditable</hideFlags>
</AudioClip>
<AudioClip>
<name>br_m_s0p0_melodi_thc</name>
<hideFlags>NotEditable</hideFlags>
</AudioClip>
<AudioClip>
<name>br_m_s0p0_melodi_choir+bell+mandolin</name>
<hideFlags>NotEditable</hideFlags>
</AudioClip>
<AudioClip>
<name>br_m_s0p0_melodi_melancholic</name>
<hideFlags>NotEditable</hideFlags>
</AudioClip>
<AudioClip>
<name>br_m_s0p0_melodi_mandolin</name>
<hideFlags>NotEditable</hideFlags>
</AudioClip>
<AudioClip>
<name>br_m_s0p0_melodi_twang_a</name>
<hideFlags>NotEditable</hideFlags>
</AudioClip>
</clips>
<initialTrack>5</initialTrack>
</melodyClips>
<drumClips>
<clips>
<AudioClip>
<name>br_m_drums_train_half</name>
<hideFlags>NotEditable</hideFlags>
</AudioClip>
<AudioClip>
<name>br_m_drums_train_fast</name>
<hideFlags>NotEditable</hideFlags>
</AudioClip>
<AudioClip>
<name>br_m_drums_train_ride</name>
<hideFlags>NotEditable</hideFlags>
</AudioClip>
</clips>
<initialTrack>1</initialTrack>
</drumClips>
<percClips>
<clips>
<AudioClip>
<name>br_m_perc_f</name>
<hideFlags>NotEditable</hideFlags>
</AudioClip>
<AudioClip>
<name>br_m_perc_i</name>
<hideFlags>NotEditable</hideFlags>
</AudioClip>
<AudioClip>
<name>br_m_perc_b</name>
<hideFlags>NotEditable</hideFlags>
</AudioClip>
<AudioClip>
<name>br_m_perc_g</name>
<hideFlags>NotEditable</hideFlags>
</AudioClip>
<AudioClip>
<name>br_m_perc_a</name>
<hideFlags>NotEditable</hideFlags>
</AudioClip>
<AudioClip>
<name>br_m_perc_d</name>
<hideFlags>NotEditable</hideFlags>
</AudioClip>
<AudioClip>
<name>br_m_perc_e</name>
<hideFlags>NotEditable</hideFlags>
</AudioClip>
<AudioClip>
<name>br_m_perc_c</name>
<hideFlags>NotEditable</hideFlags>
</AudioClip>
<AudioClip>
<name>br_m_perc_h</name>
<hideFlags>NotEditable</hideFlags>
</AudioClip>
</clips>
<initialTrack>0</initialTrack>
</percClips>
</music>
<pursuers>
<PursuerData>
<pursuerName>Sheriff</pursuerName>
<pursuerNextMarker>Challenge 2: Landslide (Fast or Slow)</pursuerNextMarker>
<pursuerPosition>
<x>507.8066</x>
<y>0</y>
<z>0</z>
</pursuerPosition>
</PursuerData>
</pursuers>
</Checkpoint>
Original Message: I am trying to serialize part of my game in order to be able to create checkpoints but I am getting the following error when I try to deserialize the file:
NullReferenceException
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:222)
Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:232)
System.Reflection.MonoProperty.SetValue (System.Object obj, System.Object value, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] index, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Reflection/MonoProperty.cs:348)
System.Reflection.PropertyInfo.SetValue (System.Object obj, System.Object value, System.Object[] index) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Reflection/PropertyInfo.cs:102)
System.Xml.Serialization.XmlTypeMapMember.SetValue (System.Object ob, System.Object value)
System.Xml.Serialization.XmlSerializationReaderInterpreter.SetMemberValue (System.Xml.Serialization.XmlTypeMapMember member, System.Object ob, System.Object value, Boolean isValueList)
System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadMembers (System.Xml.Serialization.ClassMap map, System.Object ob, Boolean isValueList, Boolean readByOrder)
System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadClassInstanceMembers (System.Xml.Serialization.XmlTypeMapping typeMap, System.Object ob)
System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadClassInstance (System.Xml.Serialization.XmlTypeMapping typeMap, Boolean isNullable, Boolean checkType)
System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadObject (System.Xml.Serialization.XmlTypeMapping typeMap, Boolean isNullable, Boolean checkType)
System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadObjectElement (System.Xml.Serialization.XmlTypeMapElementInfo elem)
System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadListElement (System.Xml.Serialization.XmlTypeMapping typeMap, Boolean isNullable, System.Object list, Boolean canCreateInstance)
System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadMembers (System.Xml.Serialization.ClassMap map, System.Object ob, Boolean isValueList, Boolean readByOrder)
System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadClassInstanceMembers (System.Xml.Serialization.XmlTypeMapping typeMap, System.Object ob)
System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadClassInstance (System.Xml.Serialization.XmlTypeMapping typeMap, Boolean isNullable, Boolean checkType)
System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadObject (System.Xml.Serialization.XmlTypeMapping typeMap, Boolean isNullable, Boolean checkType)
System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadObjectElement (System.Xml.Serialization.XmlTypeMapElementInfo elem)
System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadMembers (System.Xml.Serialization.ClassMap map, System.Object ob, Boolean isValueList, Boolean readByOrder)
System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadClassInstanceMembers (System.Xml.Serialization.XmlTypeMapping typeMap, System.Object ob)
System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadClassInstance (System.Xml.Serialization.XmlTypeMapping typeMap, Boolean isNullable, Boolean checkType)
System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadObject (System.Xml.Serialization.XmlTypeMapping typeMap, Boolean isNullable, Boolean checkType)
System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadObjectElement (System.Xml.Serialization.XmlTypeMapElementInfo elem)
System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadMembers (System.Xml.Serialization.ClassMap map, System.Object ob, Boolean isValueList, Boolean readByOrder)
System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadClassInstanceMembers (System.Xml.Serialization.XmlTypeMapping typeMap, System.Object ob)
System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadClassInstance (System.Xml.Serialization.XmlTypeMapping typeMap, Boolean isNullable, Boolean checkType)
System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadObject (System.Xml.Serialization.XmlTypeMapping typeMap, Boolean isNullable, Boolean checkType)
System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadRoot (System.Xml.Serialization.XmlTypeMapping rootMap)
System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadRoot ()
Here are my serialize/deserialize methods:
public static void WriteXML(string fileName,object obj)
{
using (var f = File.Create(fileName))
{
XmlSerializer ser = new XmlSerializer(obj.GetType());
ser.Serialize(f, obj);
}
}
public static T ReadXML<T>(string fileName)
{
using (var f = File.Open(fileName,FileMode.Open,FileAccess.Read))
{
XmlSerializer ser = new XmlSerializer(typeof(T));
return (T)ser.Deserialize(f);
}
}
The object I am trying to serialize/deserialize:
[System.Serializable]
public class Checkpoint {
public string nextMarker;
public float difficulty;
public MusicPart music;
[System.Serializable]
public struct PursuerData
{
public string pursuerName;
public string pursuerNextMarker;
public Vector3 pursuerPosition;
}
public List<PursuerData> pursuers = new List<PursuerData>();
}
Finally here is how I populate my object before serialization:
public void CreateCheckPoint()
{
checkpoint = new Checkpoint();
checkpoint.nextMarker = Player.instance.nextMarker.name;
checkpoint.difficulty = Player.instance.SpeedMultiplier;
checkpoint.music = BGMusicMixer.instance.CurrentMusic;
foreach(Pursuer p in EventListOrganizer.pursuers)
{
Checkpoint.PursuerData temp = new Checkpoint.PursuerData();
temp.pursuerName = p.name;
temp.pursuerNextMarker = p.nextMarker.name;
temp.pursuerPosition = p.transform.position;
checkpoint.pursuers.Add(temp);
}
WriteXML(filePath, checkpoint);
}
I have looked around for a solution but can't seem to find anything related to this specific issue...
What is most puzzling is that the first checkpoint of the game is deserialized without any issues but my second one throws this error on deserialization. Any ideas on what the problem might be?
Isn't AudioClip
a UnityEngine.Object
? - how can you serialize that with X$$anonymous$$L serializer? - shouldn't X (any class) be mocked with a serialization attribute for it to be serialized?
You should write your comment as an answer so I can accept it :)
Answer by whydoidoit · Mar 21, 2014 at 12:03 PM
You aren't serializing an audio clip at all - just its name and the hide flags, there's no way that is correct when you deserialize. You'd have to write some kind of management thing to find the correct clip from the project and then apply it.
Something like
using System.Linq;
var allClips = Resources.FindObjectsOfTypeAll<AudioClip>().ToDictionary(j=>j.name);
...
clip = allClips[someClipNameYouDeserialized];
You are absolutely right...I just assumed it could be done without anything else. Ill just store the name of the object holding the musicpart and fetch it that way I guess.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Convert XML Deserializer from folder to resources 2 Answers
[Possible Bug] Unity3d XmlSerializer ShouldSerialize not working? 2 Answers
Flip over an object (smooth transition) 3 Answers