Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Resolved ServerRpc method doesn't work on Client

Discussion in 'Multiplayer' started by chegodaevandrey, Nov 27, 2023.

  1. chegodaevandrey

    chegodaevandrey

    Joined:
    Oct 17, 2022
    Posts:
    4
    Hello! I've been trying to solve this issues for couple days but haven't had success so far.
    I have a script, it is attached to the Player prefab. The Player prefab, in its turn, has a Network Object component and is set as the Player prefab in Network manager.

    When I start as a host (NetworkManager.Singleton.StartHost();) everything works, but if I connect as a client (NetworkManager.Singleton.StartCleint();) the ServerRpc method doesn't work on Client.

    Please find the script below:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using Unity.Netcode;
    4. using UnityEngine;
    5.  
    6. public class TestingServerRpc : NetworkBehaviour
    7. {
    8.     void Update()
    9.     {
    10.        if (!IsOwner)
    11.             return;
    12.         if (Input.GetMouseButtonDown(0))
    13.         {
    14.             DoStuffServerRpc();
    15.         }
    16.  
    17.     }
    18.  
    19.     [ServerRpc(RequireOwnership = false)]
    20.     public void DoStuffServerRpc()
    21.     {
    22.         Debug.Log("It works!");
    23.     }
    24. }
    25.  
    Thank you!
     
  2. qenious

    qenious

    Joined:
    Feb 11, 2022
    Posts:
    9
    ServerRpc method only work in Server. If you want to send Rpc function from a Client you must use ServerRpc and ClientRpc both.

    Code (CSharp):
    1. [ServerRpc(RequireOwnership = false)]
    2. private void DoSomethingServerRpc()
    3. {
    4. DoSomethingClientRpc();
    5. }
    6.  
    7. [ClientRpc]
    8. private void DoSomethingClientRpc()
    9. {
    10. print(“Printed on all Clients”);
    11. }
     
    chegodaevandrey likes this.
  3. chegodaevandrey

    chegodaevandrey

    Joined:
    Oct 17, 2022
    Posts:
    4
    @
    Thank you very much, that helped a lot!