Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by kUr4m4 · Mar 21, 2014 at 10:39 AM · c#xmlserializer

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?

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image vexe · Mar 21, 2014 at 12:16 PM 1
Share

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?

avatar image kUr4m4 · Mar 21, 2014 at 12:17 PM 0
Share

You should write your comment as an answer so I can accept it :)

avatar image whydoidoit · Mar 21, 2014 at 12:48 PM 0
Share

Converted comment.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · Mar 21, 2014 at 12:04 PM 0
Share

Something like

  using System.Linq;

  var allClips = Resources.FindObjectsOfTypeAll<AudioClip>().ToDictionary(j=>j.name);
  ...
  clip = allClips[someClipNameYouDeserialized];
     
avatar image kUr4m4 · Mar 21, 2014 at 12:17 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges