Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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:
    7
    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!