- Home /
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
Edit: 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.
UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()
Answer by falantar14 · Apr 13, 2016 at 08:21 PM
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.
This bug is still around in 2017.3 and this fix still works, thanks.
THAN$$anonymous$$ YOU! I have been working on this issue for several days now.
Answer by Alturis2 · Nov 21, 2016 at 01:36 PM
Hmm... I just started getting this error today and I am on 5.4.2f2
I have no OnDeserialize ( NetworkReader... ) in my project
$$anonymous$$e three. Still hoping somebody with know-how finds us.
For me this ended up being because I had a networked gameObject within the DontDestroyOnLoad scene. This is apparently not supported and everything must be within the active scene.
Answer by suxinrenzs · Jun 20, 2017 at 06:49 AM
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
netMsg.reader.SeekZero();
CustMsg msg = netMsg.ReadMessage<CustMsg>();
Thank you so much for posting this! i have been having troubles for more than 3 months and this solved every thing, thank you!!
Answer by celian29 · Jan 18, 2018 at 06:38 PM
If you have this issue, try to disable the network animator. This worked for me !
Answer by jough · Aug 18, 2016 at 06:40 PM
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!