Custom Message int32 is lost in serialization

Hey guys,
I’m having yet another UNET serialization issue. I’m currently making a networked CCG with UNET and am using custom messages for much of my communication.

However now one message is not returning the correct value from the NetworkReader.ReadInt32() method.

Here’s the code:

public void RequestSetDeck(List<Card> deck_, int playerID_)
    {
        the_client.Send(GameConfig.DeckNetworkMessage, new DeckMessage(deck_, playerID_));
    }

    private void SERVER_OnSetDeckRequestRecieved(NetworkMessage netMsg)
    {
        DeckMessage m = netMsg.ReadMessage<DeckMessage>();
        Debug.Log(m.playerID);
        Debug.Log(m.cards[29].id);
        try
        {
            if (m.playerID == 0)
            {
                instance.the_match.player1.SetDeck(m.cards);
                instance.RpcDeckSetConfirmed(m.playerID);
            }
            else if (m.playerID == 1)
            {
                instance.the_match.player2.SetDeck(m.cards);
                instance.RpcDeckSetConfirmed(m.playerID);
            }

        }
        catch (DeckInvalidException e)
        {
            instance.RpcDeckSetDenied(m.playerID);
        }
        catch (Exception e)
        {
            Debug.LogException(e);
        }
    }

Debug.Log(m.playerID) writes 3474792 to the console log eventhough playerID in RequestSetDeck is 0. If I hardcode a number into the Serialize method in my DeckMessage class the Log still prints 3474792.

Debug.Log(Debug.Log(m.cards[29].id)) throws an IndexOutOfBounds Exception eventhough the list during serialization has 30 entries.

Here is my Message class:

public class DeckMessage : MessageBase
    {
        public List<Card> cards;
        public int playerID;

        public DeckMessage () { }
        public DeckMessage(List<Card> data_, int playerID_)
        {
            cards = new List<Card>();
            playerID = playerID_;
            foreach (Card c in data_)
            {
                cards.Add(c);
            }
        }

        public override void Serialize(NetworkWriter writer)
        {
            writer.StartMessage(GameConfig.DeckNetworkMessage);

            writer.Write(playerID);

            for (int i = 0; i < GameConfig.deckSize; i++)
            {
                if (i >= cards.Count || cards[i] == null)
                {
                    writer.Write(false);
                    continue;
                }
                else
                {
                    writer.Write(true);
                    cards[i].Serialize(writer);
                }

            }
            writer.FinishMessage();
        }

        public override void Deserialize(NetworkReader reader)
        {
            this.cards = new List<Card>();

            this.playerID = reader.ReadInt32();

            for (int i = 0; i < GameConfig.deckSize; i++)
            {
                if (!reader.ReadBoolean())
                {
                    continue;
                }
                else
                {
                    cards.Add(Card.Deserialize(reader));
                }
            }
        }
    }

This pattern was working fine for other classes which had more data. Not sure what’s causing this. Any help would be appreciated!

I’m bumping this since I’ve managed to narrow down the problem. Still haven’t found a fix yet though.
Apparently the ReadInt32() Method is reading parts of the header from the message.

If I change the MessageType from MsgType.Highest + 6 to something else the debugged number changes. No other modifications do anything though. Even commenting out the entire for-loop so I’m in essence just reading/writing the one int continues to be buggy.

Okay, I’ve managed to find a workaround and what the issue is. Still not sure what causes it though.

Apparently when sending a NetworkMessage from client to server the Serialize function writes the message ID at the start of the message and the Deserialize function starts reading at that message ID.

This is not the case when sending from server to client.

public override void Deserialize(NetworkReader reader)
        {
            this.cards = new List<Card>();
// read 4 bytes to skip the int32 written containing the message ID
            reader.ReadBytes(4);
            this.playerID = reader.ReadInt32();

            for (int i = 0; i < GameConfig.deckSize; i++)
            {
                if (!reader.ReadBoolean())
                {
                    continue;
                }
                else
                {
                    cards.Add(Card.Deserialize(reader));
                }
            }
        }

This works as intended. However, I’m pretty sure this entire thing is a bug.