Unity-Technologies / com.unity.netcode.gameobjects

Netcode for GameObjects is a high-level netcode SDK that provides networking capabilities to GameObject/MonoBehaviour workflows within Unity and sits on top of underlying transport layer.
MIT License
2.15k stars 435 forks source link

InvalidCastException on serverRpc call on Unity Editor host #1110

Closed GuilhermeGSousa closed 3 years ago

GuilhermeGSousa commented 3 years ago

Describe the bug A InvalidCastException: Specified cast is not valid. exception is raised when a ServerRpc is called by a connected client.

To Reproduce With a host on the Unity Editor and a build running a client, call a serverRpc like this one:

  [ServerRpc(RequireOwnership = false)]
  private void ClientIsReadyServerRpc(ulong clientId)

Note that this error does not happen if both the host and client run on builds, nor if the client runs on the editor and host on build.

Actual outcome

NetworkManager.cs:1145 raises an InvalidCastException, on the following line of code:

__ntable[networkMethodId](networkBehaviour, new NetworkSerializer(queueItem.NetworkReader), rpcParams);

Error log:

InvalidCastException: Specified cast is not valid.
LobbyControl.OnClientIsReadyServerRpc__nhandler (MLAPI.NetworkBehaviour target, MLAPI.Serialization.NetworkSerializer serializer, MLAPI.Messaging.__RpcParams rpcParams) (at <496a9797041240fa8a7c3c1ccc62dffb>:0)
MLAPI.NetworkManager.InvokeRpc (MLAPI.Messaging.RpcFrameQueueItem queueItem) (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Core/NetworkManager.cs:1145)
MLAPI.Messaging.RpcQueueProcessor.ProcessReceiveQueue (MLAPI.NetworkUpdateStage currentStage) (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Messaging/RpcQueue/RpcQueueProcessor.cs:63)
MLAPI.Messaging.RpcQueueContainer.ProcessAndFlushRpcQueue (MLAPI.Messaging.RpcQueueContainer+RpcQueueProcessingTypes queueType, MLAPI.NetworkUpdateStage currentUpdateStage) (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Messaging/RpcQueue/RpcQueueContainer.cs:114)
MLAPI.Messaging.RpcQueueContainer.NetworkUpdate (MLAPI.NetworkUpdateStage updateStage) (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Messaging/RpcQueue/RpcQueueContainer.cs:62)
MLAPI.NetworkUpdateLoop.RunNetworkUpdateStage (MLAPI.NetworkUpdateStage updateStage) (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Core/NetworkUpdateLoop.cs:148)
MLAPI.NetworkUpdateLoop+NetworkUpdate+<>c.<CreateLoopSystem>b__0_0 () (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Core/NetworkUpdateLoop.cs:207)

Expected outcome The ServerRpc should be called without raising an exception.

Screenshots N/A

Environment (please complete the following information):

Additional context I recently changed to the new Unity Hub beta, I had this error for the first time when running this code after that change. The same code would work fine before that. I may update this issue with a minimal project to reproduce the error if I have some time available.

0xFA11 commented 3 years ago

Do you mind trying to reproduce this on latest develop branch instead of master 0.1.0?

GuilhermeGSousa commented 3 years ago

Hi @MFatihMAR, I just switched to develop (c1ee3b62), and I get the same behaviour (no server rpc is called), but no exception is raised this time.

0xFA11 commented 3 years ago

hmm, interesting. can you attach some code snippets from your scripts so that we could reproduce this issue locally on our side too?

GuilhermeGSousa commented 3 years ago

I'll try to setup a minimalistic project that reproduces the error today

GuilhermeGSousa commented 3 years ago

I couldn't reproduce the bug on a new project, and after some refactoring/code cleanup on my own project the exception disapeared. In hindsight it was probably related to the fact that I was starting up the client (NetworkManager.Singleton.StartClient()) on a scene that had other NetworkObjects. As I am unable to reproduce this in a controlled environment I'll leave to your discretion weather to close this issue.

0xFA11 commented 3 years ago

ugh, it's sad to hear that we reached to a dead end. one more question though, did you upgrade from 0.1.0 (master branch) to latest develop and got these issues in the project? or was it a fresh package install either 0.1.0 master or latest develop? that might give us a clue about what is not cleaning up after itself.

GuilhermeGSousa commented 3 years ago

No I was already on the 0.1.0 release when I had the exception raised, I then upgraded to the latest develop and had the same issue, without the exception. If you wish to invesigate further I can give the exact version of my personal project that had these issues, it is honestly the only way I can think of to reproduce this.

0xFA11 commented 3 years ago

I see. I think 0.1.0 to develop transition wouldn't be super smooth with ILPP and cached assemblies. deleting Library and Temp folder would force Unity to recompile/reimport and I believe that'd fix issues you were having. obviously, not the best solution but hopefully we won't have these weird issues again in the future :)

thank you very much for following up on this and letting us be aware of it.