INetworkSerializable, TypeLoadException: Invalid generic instantiation

I am trying to serialize a class, but I keep getting this error. What does it mean?

These are the attributes of the class.

protected int attack;
protected int damage;
protected int evasion;
protected int defense;
protected int hitPoints;

protected int garrisonMax;
protected int garrison;

protected SettlementType type;
protected GameObject garrisonDisplay;
protected Tile tile;
protected Combat combat;

I am trying to serialize these fields, I was told the class doesnt have to serialize all its attributes, right?

public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
{
      // Serialize int fields
      serializer.SerializeValue(ref attack);
      serializer.SerializeValue(ref damage);
      serializer.SerializeValue(ref evasion);
      serializer.SerializeValue(ref defense);
      serializer.SerializeValue(ref hitPoints);
      serializer.SerializeValue(ref garrisonMax);
      serializer.SerializeValue(ref garrison);
}

I get the error only when trying to use the class in a networkvariable like this. Even when only declared it's immediatelly givving the error.

NetworkVariable<SettlementBase> settlementBaseNetwork = new();

What should I do?

Are any of these fields a generic type or contain a generic type? That‘s what I would be looking into given the error message.

I had this error when trying to implement INetworkSerializable in a class, as you seem to be doing.

Got past it by changing the class to a struct.

Not sure if we can Network Serialize classes - would like to know.

Good point. The docs only use struct in their example but it doesn’t explicitly mention that struct is a requirement.

There is also INetworkSerializeByMemcpy which naturally only works with a struct. And then there is a special managed serializer (?) further suggesting that INetworkSerializable is either for struct only, or that you have to do something else to serialize a managed class.