In order to store my data I created a serializable class.
I want to Serialize a class that contain a Transform. Of course I don’t want to serialize the Transform, only the others variables. So I can make this Transform private. But in order to access to the private Transform via a script, I have make a Getter/Setter public function, and of course that’s create this 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).
How to sove my problem? (except create another script that contain my serialize class and my Transform separate)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
[System.Serializable]
public class MyDataClass
{
public string Data1;
public string Data2;
public string Data3;
private Transform transfromToNotSerialize;
public Transform TransfromToNotSerialize
{
get
{
return transfromToNotSerialize;
}
set
{
transfromToNotSerialize = value;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
[System.Serializable]
public class MyDataClass
{
public string Data1;
public string Data2;
public string Data3;
private Transform transfromToNotSerialize;
[NonSerialized]
public Transform TransfromToNotSerialize
{
get
{
return transfromToNotSerialize;
}
set
{
transfromToNotSerialize = value;
}
}
}
And it’s give me this error:
error CS0592: The attribute `System.NonSerializedAttribute' is not valid on this declaration type. It is valid on `field' declarations only
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
[System.Serializable]
public class MyDataClass
{
public string Data1;
public string Data2;
public string Data3;
[NonSerialized]
public Transform transfromToNotSerialize;
}
I get this 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).
You’re placing the attribute over the property and not the field. Properties themselves are not serialized; it is the fields that are. So you need to place your attribute over
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
[System.Serializable]
public class MyDataClass
{
public string Data1;
public string Data2;
public string Data3;
[NonSerialized]
public Transform transfromToNotSerialize;
}
but I always have this 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).
I add [System.Serializable] on the top of my class.
Well like I asked, how are you trying to serialize the class? Serializable attribute is for the Binary serializer and it seems like you’re trying to use XML.
And that’s work perfectly fine, thanks a lot Supremegrandruler
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
[System.Serializable]
public class MyDataClass
{
public string Data1;
public string Data2;
public string Data3;
[System.Xml.Serialization.XmlIgnore]
public Transform transfromToNotSerialize;
}