InvalidOperationException: The type of the argument object 'Scratch' is not primitive.

So there’s the error. What I’m doing is serializing a list of classes (List). The BaseEnemy class also has a list in it of classes (List). When I run things, the list of BaseEnemy serializes properly. However, the list of BaseMoves does not. Is it a problem because BaseMoves is a list of classes inside of a class? If so, can anybody point me in the direction of the workaround?

EDIT - Forgot to mention. “Scratch” is a class that derives from BaseMove that is stored in the List.

Here’s the function that handles saving:

private void SaveEnemyRoster(string path, object obj){
        using (TextWriter writer = new StreamWriter(path)){
            Type[] extraTypes = {typeof(List<BaseMove>)};
            XmlSerializer xml = new XmlSerializer(typeof(List<BaseEnemy>), extraTypes);
            xml.Serialize(writer, obj);
        }
    }

The error is connected to line 5.

x_x Found an answer to the problem on stackoverflow.com.

“Mark Units class with XmlInclude attribute passing your derived class as parameter:”

[XmlInclude(typeof(Scratch))]
public class BaseMove
{
    public BaseMove()
    {
    }
}

It does indeed work. Now the only problem is that I’ll have to do that for EVERY SINGLE move there is, and there are hundreds. There’s got to be a shortcut. x_x

1 Like

Thank you!

Welcome to the forums but please use the “Like” button to show appreciation so you don’t necro the thread.

For more information, see here: https://discussions.unity.com/t/940149

Thanks.

Thread Closed.