Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved ServerRPC InvalidCastException

Discussion in 'Multiplayer' started by PSYCHEER, Sep 25, 2022.

  1. PSYCHEER

    PSYCHEER

    Joined:
    Sep 4, 2016
    Posts:
    2
    Hi everyone, I have some problem with serverRPC using Netcode for gameobjects.

    I have some basic player controller script and then this
    Code (CSharp):
    1.  
    2.  
    3.    [HideInInspector]
    4.    public NetworkVariable<Vector3> networkPosition = new NetworkVariable<Vector3>(default, NetworkVariableReadPermission.Everyone, NetworkVariableWritePermission.Owner);
    5.    [HideInInspector]
    6.    public NetworkVariable<Vector3> networkRotation = new NetworkVariable<Vector3>(default, NetworkVariableReadPermission.Everyone, NetworkVariableWritePermission.Owner);
    7.  
    8.     [ServerRpc(RequireOwnership = false)]
    9.     public void MyMovement_ServerRpc(Vector3 pos, Vector3 rot)
    10.     {
    11.         //var client = NetworkManager.ConnectedClients[OwnerClientId];
    12.         //client.PlayerObject.transform.position = pos;
    13.      
    14.         Debug.Log("Position: " + pos + ", Rotation: " + rot);
    15.         //client.PlayerObject.transform.eulerAngles = rot;
    16.     }
    17.    
    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
     

    Attached Files:

    Last edited: Sep 25, 2022
  2. GrindingGoat

    GrindingGoat

    Joined:
    Sep 26, 2020
    Posts:
    2
    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.
     
  3. PSYCHEER

    PSYCHEER

    Joined:
    Sep 4, 2016
    Posts:
    2
    no i have in one scene one networkbhv. so only in game, in menus not. idk why its caused
     
  4. Peanut_Butcher

    Peanut_Butcher

    Joined:
    Nov 18, 2020
    Posts:
    3
    Same here... Simple ClientRpc call on server side, without any parameters, giving me the same error
     
  5. Seskon

    Seskon

    Joined:
    Sep 29, 2020
    Posts:
    1
    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 :)
     
    Maickonn likes this.
  6. unity_Makar

    unity_Makar

    Joined:
    Oct 5, 2022
    Posts:
    1
    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.
     
  7. Banki2

    Banki2

    Joined:
    Oct 13, 2023
    Posts:
    1
    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.
     
  8. Maickonn

    Maickonn

    Joined:
    Apr 9, 2020
    Posts:
    1
    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.