- Home /
Error when trying to save inventory with array.
I am using http://www.unifycommunity.com/wiki/index.php?title=Save_and_Load_from_XML to save and load.
i have an inventory with array. That is class that contains items vars. This form Inv_Inventory from Player:
class InventoryItem
{
var worldObject: GameObject;
var itemIcon: Texture;
var itemName : String;
var itemDescription : String;
var Stack : int;
var MaxStack : int;
}
This From SaveLoad:
if (GUI.Button(_Save,"Save")) {
GUI.Label(_SaveMSG,"Saving to: "+_FileLocation);
//Debug.Log("SaveLoadXML: sanity check:"+ _Player.transform.position.x);
var plr = gameObject.Find("Player");
var invScrpt = plr.GetComponent(Inv_Inventory);
myData._iUser.x = _Player.transform.position.x;
myData._iUser.y = _Player.transform.position.y;
myData._iUser.z = _Player.transform.position.z;
myData._iUser.name = _PlayerName;
for( var j = 0; j < myData._iUser.inventory.length; j ++ )
{
for( var s = 0; s < myData._iUser.inventory[j].length; s ++ )
{
for( var i = 0; i < invScrpt.inventory.length; i ++ )
{
for( var k = 0; k < invScrpt.inventory[i].length; k ++ )
{
if (invScrpt.inventory[i][k] != null)
{
myData._iUser.inventory[j][s] = invScrpt.inventory[i][k];
}
}
}
}
}
// Time to creat our XML!
_data = SerializeObject(myData);
// This is the final resulting XML from the serialization process
CreateXML();
Debug.Log(_data);
}
Like That i am trying to save Inventory Array to file.
But when i click save and have item in inventory i have this error.
InvalidOperationException: The type of the argument object 'Inv_Inventory.InventoryItem' is not primitive.
Help me plz guys. I don't know what to do.
Answer by Ricardo Arango · Jun 15, 2011 at 11:56 AM
You can only serialize primitive types directly. If you are not using Primitive types you should implement the ISerializable attribute with the methods used for serialization.
Have a look at the ISerilizable docs to understand this a bit better:
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.iserializable.aspx
In you code the InventoryItem class is not serializable. And some of the members of the class are not either (GameObject or Texture)