UNet IndexOutOfRangeException: NetworkReader:ReadBytes out of range error when spawning a 2nd Player

I have a multiplayer game and when I launch the host, I can select a character and he spawns with no problem. But when I try and join the game on another (local) client, the character gets created in the scene at (0,0,0) but then I get this fatal error every time:

IndexOutOfRangeException: NetworkReader:ReadBytes out of range: (25908) NetBuf sz:280 pos:204
UnityEngine.Networking.NetBuffer.ReadBytes (System.Byte[] buffer, UInt32 count) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkBuffer.cs:45)
UnityEngine.Networking.NetworkReader.ReadString () (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkReader.cs:302)
SyncListItem.DeserializeItem (UnityEngine.Networking.NetworkReader reader)
Unity.GeneratedNetworkCode._ReadStructSyncListItem_None (UnityEngine.Networking.NetworkReader reader, .SyncListItem instance)
Player.OnDeserialize (UnityEngine.Networking.NetworkReader reader, Boolean initialState)
UnityEngine.Networking.NetworkIdentity.OnUpdateVars (UnityEngine.Networking.NetworkReader reader, Boolean initialState) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkIdentity.cs:753)
UnityEngine.Networking.ClientScene.ApplySpawnPayload (UnityEngine.Networking.NetworkIdentity uv, Vector3 position, System.Byte[] payload, NetworkInstanceId netId, UnityEngine.GameObject newGameObject) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/ClientScene.cs:478)
UnityEngine.Networking.ClientScene.OnObjectSpawn (UnityEngine.Networking.NetworkMessage netMsg) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/ClientScene.cs:533)
UnityEngine.Networking.NetworkConnection.HandleReader (UnityEngine.Networking.NetworkReader reader, Int32 receivedSize, Int32 channelId) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkConnection.cs:430)
UnityEngine.Networking.NetworkConnection.HandleBytes (System.Byte[] buffer, Int32 receivedSize, Int32 channelId) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkConnection.cs:386)
UnityEngine.Networking.NetworkConnection.TransportRecieve (System.Byte[] bytes, Int32 numBytes, Int32 channelId) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkConnection.cs:536)
UnityEngine.Networking.NetworkClient.Update () (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkClient.cs:648)
UnityEngine.Networking.NetworkClient.UpdateClients () (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkClient.cs:861)
UnityEngine.Networking.NetworkIdentity.UNetStaticUpdate () (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkIdentity.cs:1059)

I’ve been trying to track down exactly where this happens but to no avail.

Note: I am using a package from the asset store that extends NetworkManager.cs

I’m also getting this error when I run the game. I’m not sure if it has anything to do with it.

NetworkManager detected a script reload in the editor. This has caused the network to be shut down.

I can’t imagine this is anything but a bug. I managed to fix it by overriding the OnDeserialize() method in my Player class that extends the NetworkBehaviour class.

    public override void OnDeserialize(NetworkReader reader, bool initialState)
        base.OnDeserialize(reader, initialState);

And the error is no longer appearing.

Hmm… I just started getting this error today and I am on 5.4.2f2

I have no OnDeserialize ( NetworkReader… ) in my project

I was having the same problem when I read the NetworkMessage use code like this:

CustMsg msg = netMsg.ReadMessage<CustMsg>();

Solve the problem by adding the following code in front

CustMsg msg = netMsg.ReadMessage<CustMsg>();

If you have this issue, try to disable the network animator. This worked for me !

For those of you coming to this forum looking for answers…

Using 5.3.x this was a potential fix, however with 5.4 I have came to the realization that this actually causes the IndexOutOfRangeException: NetworkReader:ReadBytes error to occur.

So just to reiterate, those experiencing the issue in 5.4 should get rid of any

public override void OnDeserialize(NetworkReader reader, bool initialState) { base.OnDeserialize(reader, initialState); }

calls they may have throughout the project.

Hope this helps!

As a side note, I was getting the same error but with the size 0, pos 0. I.e. my messages were null. I was on

I upgraded to 5.5.2 and the error dissapeared.

This is still an issue in 2018.1.0f2
I ended up using a try/catch. SeekZero only works for some messages, while it causes an Exception for others. Seems to depend on the serialized types.

    private void CustomMessageHandler(NetworkMessage netMsg) {
        CustomMessage message;
            message = netMsg.ReadMessage<CustomMessage>();
        catch (Exception e)
            message = netMsg.ReadMessage<CustomMessage>();

I had the problem because there were 2 different unity apps, server and client. The advanced channel configuration on server did not match that of client. Server had 1 channel more in the “Qos channels” list in scene.