Trying to XML serialize objects, getting error that UnityEngine.Transform doesn't implement Add()

So I’m trying to serialize an object via XML serialization for saving and loading.

However, when I try to save (serialize) the object, I get this exception:

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).

Since that is from (I’m assuming) a precompiled library (a DLL), how can I get around this issue? If it were one of my own functions I’d go implement an Add() function to the offending class, but I don’t think I have that option. What can I do?

Was about to say that Transform is not an IEnumerable, but it is. :stuck_out_tongue:

However, IEnumerable doesn’t require the implementation of Add(). That’s normally ICollection’s implementation.

It’s very strange, but it could be a limitation of your XML Serializer that assume any IEnumerable must have Add() so it could rebuild it later on. It probably assumes that it serializes any IEnumerable by using it’s Enumerator, and rebuild it by re-adding every object one by one.

You could always use Unity’s serialization, or if you truly want to build your own serialization, you could use a wrapping class to do the translation between XML and Unity.