Search Unity

Calling ServerRpc in ClientRpc, but it has no ownership

Discussion in 'Netcode for GameObjects' started by unity_1347539, Feb 13, 2022.

  1. unity_1347539

    unity_1347539

    Joined:
    Dec 18, 2021
    Posts:
    1
    In the project, I am currently trying to get certain networkObjects in a ServerRpc, then tell these netowkrObjects to perform some tasks. The flow is as follows

    Code (CSharp):
    1.  
    2. [ServerRpc]
    3. private void getPlayersServerRpc() {
    4.     List<ulong> targetClientId = new List<ulong>();
    5.     /* create clientRpcParams and add clientId to targetClientId */
    6.     clientRpcParams.Send.TargetClientIds = targetClientId.ToArray();
    7.     notifyPlayersClientRpc(clientRpcParams);
    8. }
    9.  
    10. [ClientRpc]
    11. private void notifyPlayersClientRpc(ClientRpcParams clientRpcParams = default) {
    12.     doTasksServerRpc();  /* <------ error */
    13. }
    14.  
    15. [ServerRpc]
    16. private void doTasksServerRpc() {
    17.     /* change position and color */
    18. }
    19.  
    I started by calling getPlayersServerRpc(). I believe it stops when trying to call doTasksServerRpc() in notifyPlayersClientRpc(). The error message says "Only the owner can invoke a ServerRpc that requires ownership!" which I have no idea how to fix. But when I tried to print something in notifyPlayersClientRpc(). The printout appeared in the correct client console.

    So, please provide me with a solution. Thank you
     
  2. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    666
    A quick way to remedy sending an RPC from a client without ownership is to use [ServerRpc(RequireOwnership = false)].
     
  3. Officer_Yoda

    Officer_Yoda

    Joined:
    Nov 17, 2021
    Posts:
    1
  4. cosmochristo

    cosmochristo

    Joined:
    Sep 24, 2018
    Posts:
    250
    Last edited: Oct 14, 2022
  5. supernaturallife27

    supernaturallife27

    Joined:
    Dec 6, 2022
    Posts:
    1
    i think i love you too
     
  6. Catetoababor

    Catetoababor

    Joined:
    Aug 28, 2021
    Posts:
    1
    i love you too
     
  7. Spankie

    Spankie

    Joined:
    Oct 1, 2019
    Posts:
    2