Script with XML fields won't allow seperate GameObject variable :(

I’m reading my item (swords and such) variables from a XML file.

It all works perfectly, but the Item class that gets fed values from the item database XML file also has one variable that is only used during runtime: worldItem (GameObject).

worldItem holds an instantiated prefab when the item is equipped and thus visible on a character instead of being invisible in the backpack.

Now, when running the game I get an error:

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

(and so on).

The thing is, I don’t understand why this variable which is never touched by any XML stuff gives an error. Is there any way to have the script hold a gameobject variable? I do not want to XML serialize a gameobject or anything like that.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;


[System.Serializable]
public class Item {
     [XmlAttribute("name")]
     public string name = "";
     [XmlAttribute("value")]
     public int value = 0;

     public GameObject worldItem;//this is the "bad line" :)
}

1 Answer

1

I just found the answer :slight_smile: Since I didn’t want to serialize the Item class anyway, I just needed to add “[XmlIgnore]” before the gameobject variable declaration.