question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

InvalidCastException on serverRpc call on Unity Editor host

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
GuilhermeGSousacommented, Sep 1, 2021

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.

1reaction
GuilhermeGSousacommented, Sep 1, 2021

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Question - ServerRPC InvalidCastException
Hi everyone, I have some problem with serverRPC using Netcode for gameobjects. I have some basic player controller script and then this...
Read more >
Specified cast is not valid." While making custom network ...
I am trying to make a custom network manager for my multiplayer game. What I want to do is have the first player...
Read more >
Cannot cast from source type to destination type : r/Unity3D
Invalid cast exception: Cannot cast from source type to destination type. This error happens on 2 occasions: 1: if (Menu.hairType != string.
Read more >
Netcode for GameObjects (Netcode) Error Messages
Error messages are captured and returned through Unity Editor ... Server: [Netcode] Only owner can invoke ServerRPC that is marked to ...
Read more >
Unity Mirror Networking InvalidCastException: Specified ...
I found a solution i moved my Spawn code to a function that is OnServerAddPlayer instead of OnClientConnect it fixes all the problems...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found