Serializing a List

Making and RPG and trying to save a List of Usable’s that implement the IComparable interface, but get this error when trying to serialize it

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

I’ve tried adding the Add function to all levels that that doesn’t seem to work.

using System;

[Serializable]
public class Usable : Item, IComparable<Usable>
{
    public string prompt;

    public StatusEffect statusEffect;

    public enum Target { Single, Party }
    public Target target;

    public Usable() { }

    public Usable(UsablePreset usablePreset)
    {
        itemName = usablePreset.itemName;
        prompt = usablePreset.prompt;
        statusEffect = usablePreset.statusEffect;
        target = usablePreset.target;
    }

    public int CompareTo(Usable other)
    {
        if (other == null)
            return 1;

        return price - other.price;
    }

    public void Add(Usable usable)
    {

    }
}
[System.Serializable]
public class Item
{
    public string itemName;
    public int spriteIndex;
    public int price, value;

    public void Add(Usable usable)
    {

    }
}

Long story short, Unity can’t serialize lists of interfaces (or interfaces at all, I believe). Not without using serialization plugins like Odin Inspector.

There is the [SerializeReference] attribute but it has all sorts of caveats around it. Overall I found it terrible to use. You’d also have to write custom UI for it. In the end, a big disappointment. Cool it is possible, but obnoxious to use.
You’re indeed better off with Odin, but of course Odin is not free and you’ll be stuck with it in the project. If you were to remove Odin, it won’t be easy. So if Odin were to be incompatible with a newer version of Unity, you’d have to wait for them to fix it. It’s a dependency.

I wish Unity did more about serialization. Because the serialization system is quite limited out of the box. There are so many things you could do with serializing interfaces…

Also XML does the job but is slow in serializing big XML’s
If you were to work a big RPG world in it, I’d suggest Json rather than XML

Based on my experience, Unity default SerializeReference also doesn’t work on Interfaces. Not that Unity can even expose SerializeReference fields despite being able to save the data on the asset.

Like I said, it’s with caveats. You’d have to create a custom UI for it and it doesn’t work out of the box like this.
I’ve got a project that uses the SerializeReference attribute on a few classes that implement interfaces. Those interface properties become serializable… but you’ll have to create an instance yourself through a custom UI.
It is quite tedious to work with.

And this is exactly why I use Odin.

Is the interface really required? Why not encapsulate a Serializable object into an object that adheres to an Interface?

You won’t need external libraries, don’t add dependencies, and you’ll still be leveraging the capacities of an OOP language. It might require some rework of existing code though.

Maybe I don’t understand something, but unity does not use XML to serialize things, therefore I’m not even sure if guy uses unity serialization at all.
Isn’t the problem that you’re implementing “void Add(Usable)” instead of “void Add(object)”?