ServerRPC InvalidCastException

Hi everyone, I have some problem with serverRPC using Netcode for gameobjects.

I have some basic player controller script and then this

   [HideInInspector]
   public NetworkVariable<Vector3> networkPosition = new NetworkVariable<Vector3>(default, NetworkVariableReadPermission.Everyone, NetworkVariableWritePermission.Owner);
   [HideInInspector]
   public NetworkVariable<Vector3> networkRotation = new NetworkVariable<Vector3>(default, NetworkVariableReadPermission.Everyone, NetworkVariableWritePermission.Owner);

    [ServerRpc(RequireOwnership = false)]
    public void MyMovement_ServerRpc(Vector3 pos, Vector3 rot)
    {
        //var client = NetworkManager.ConnectedClients[OwnerClientId];
        //client.PlayerObject.transform.position = pos;
     
        Debug.Log("Position: " + pos + ", Rotation: " + rot);
        //client.PlayerObject.transform.eulerAngles = rot;
    }

after all, only code used I have is Debug.Log

On other side(server-side which is currently only on localhost) is giving me an error

InvalidCastException: Specified cast is not valid.
PlayerController.__rpc_handler_1475604677 (Unity.Netcode.NetworkBehaviour target, Unity.Netcode.FastBufferReader reader, Unity.Netcode.__RpcParams rpcParams) (at <c6ba7056cfda45d8b1dc56b2e2d5658e>:0)
Unity.Netcode.RpcMessageHelpers.Handle (Unity.Netcode.NetworkContext& context, Unity.Netcode.RpcMetadata& metadata, Unity.Netcode.FastBufferReader& payload, Unity.Netcode.__RpcParams& rpcParams) (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.2/Runtime/Messaging/Messages/RpcMessages.cs:77)
Rethrow as Exception: Unhandled RPC exception!
UnityEngine.Debug:LogException(Exception)
Unity.Netcode.RpcMessageHelpers:Handle(NetworkContext&, RpcMetadata&, FastBufferReader&, __RpcParams&) (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.2/Runtime/Messaging/Messages/RpcMessages.cs:81)
Unity.Netcode.ServerRpcMessage:Handle(NetworkContext&) (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.2/Runtime/Messaging/Messages/RpcMessages.cs:122)
Unity.Netcode.MessagingSystem:ReceiveMessage(FastBufferReader, NetworkContext&, MessagingSystem) (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.2/Runtime/Messaging/MessagingSystem.cs:457)
Unity.Netcode.MessagingSystem:HandleMessage(MessageHeader&, FastBufferReader, UInt64, Single, Int32) (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.2/Runtime/Messaging/MessagingSystem.cs:387)
Unity.Netcode.MessagingSystem:ProcessIncomingMessageQueue() (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.2/Runtime/Messaging/MessagingSystem.cs:407)
Unity.Netcode.NetworkManager:OnNetworkEarlyUpdate() (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.2/Runtime/Core/NetworkManager.cs:1541)
Unity.Netcode.NetworkManager:NetworkUpdate(NetworkUpdateStage) (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.2/Runtime/Core/NetworkManager.cs:1473)
Unity.Netcode.NetworkUpdateLoop:RunNetworkUpdateStage(NetworkUpdateStage) (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.2/Runtime/Core/NetworkUpdateLoop.cs:185)
Unity.Netcode.<>c:<CreateLoopSystem>b__0_0() (at Library/PackageCache/com.unity.netcode.gameobjects@1.0.2/Runtime/Core/NetworkUpdateLoop.cs:208)

Here is a whole script

8464439–1124174–PlayerController.cs (5.51 KB)

I had the exact same issue and couldnt find a real solution for it. Do you have multiple Network Behaviour scripts on your object that use ServerRpcs?
In my case I moved the logic of the not working Rpc to a seperate Gameobject, that gets spawned along with the player.

no i have in one scene one networkbhv. so only in game, in menus not. idk why its caused

Same here… Simple ClientRpc call on server side, without any parameters, giving me the same error

Had the same exception (occuring only on the client side) caused when i wanted to send a move command from my Server to the Clients (ClientRpc).

I managed to fix it now by removing some Destroy() calls that destroyed components only on the client side (of the same Networkobject). It probably changed the NetworkObject in an unrecognizable way to the server, but I didnt look into it so it could also be another reason, but maybe this helps somone :slight_smile:

1 Like

I had the same problem. I tried to fix it for quite a long time, and in the end it was caused by the fact that the object had one script on the server (which was not even involved in the process), but on the client I destroyed it at initiation (so that it would not be accidentally called on the client). I stopped destroying this script on the client and the error no longer appeared.

It seems like you’ve encountered a programming issue related to a script on the server and client side in your code. It’s great that you were able to identify the root cause of the problem and find a solution by not destroying the script on the client side. This kind of debugging and troubleshooting is an essential part of the development process.

Greetings! I recently encountered a similar issue, and it appears that the problem lies in the use of “Destroy(this)” on line 98. When you remove the script at runtime and subsequently call an RPC, it leads to a breakage. This is because the RPC method is no longer available on the other end due to the script being destroyed.