Serialization Object fail!

Hi at all,

I try to serialize an Object that has an array inside its, like this:

class TerritoriesWrapper{
	
	var list : Territory[];

	function 	TerritoriesWrapper(){
		
		return;	
	}
	
	function TerritoriesWrapper(array : Territory[]){
	
		this.list = array;
		return;	
		
	}
	
	
}

with the following code:

var temp :TerritoriesWrapper = new TerritoriesWrapper(territories);
var xml = new XmlSerializer(TerritoriesWrapper);
xml.Serialize(this.stream,temp);

But I get this error:

InvalidOperationException: The type of the argument object is not primitive.
System.Xml.Serialization.XmlSerializer.Serialize (System.IO.TextWriter textWriter, System.Object o) 
Save.saveTerritories (.Territory[] territories) [0x00017]  (at Assets/Scripts/SaveAndLoadClasses.js:27)
MyGame.saveGame (.Territory[] territories) [0x00000]  (at Assets/Scripts/GameClass.js:202)
SceneController+NextPlayer$136+$.MoveNext ()   (at Assets/Scripts/SceneController.js:284   at System.Xml.Serialization.XmlSerializationWriter.WriteTypedPrimitive (System.String name, System.String ns, System.Object o, Boolean xsiType) [0x00000])

Doesn’t possible to serialize Object with array of object or I have wrong somethings?

Thank at all in advance.

I have finded the error!

It’s because GameObjects inside a TerritoryWrapper doesn’t primitive!

Seeing similar here. Did you find a solution to this?

I’m not sure, but it sounds like he was indirectly serializing GameObjects, which as far as I know, cannot be serialized.

Indeed.

Myself, was getting a Vector4 “is not primitive” error on Serialize, and ended up writing a Serializable Vector4 class but still received the same error.

Finally just wrote out the Vector4 arrays as separate float arrays and all was well.

Would love to see at least Unity “primitives” like Vector2, 3, 4 be automagically Serializable for mono! :slight_smile:

Hey Joe,
I have the same problem with Vector3. Can you share the code for putting the positions in float array with me/us?

Thx in advance

var point : Vector3 = new Vector3(1, 2, 3);
var pointArray : float[] = [point.x, point.y, point.z];
var deserializedPoint : Vector3 = new Vector3(pointArray[0], pointArray[1], pointArray[2]);

http://forum.unity3d.com/threads/34015-Newbie-guide-to-Unity-Javascript-(long)

I write in c# but this helps alredy thx for it.