Search Unity

Method with ServerRpc not being called

Discussion in 'Netcode for GameObjects' started by buckeyes622, Oct 19, 2022.

  1. buckeyes622

    buckeyes622

    Joined:
    Aug 15, 2019
    Posts:
    2
    Pretty simply, I have two methods that I want to run when a button is clicked. The first functions properly. The second works up until I add the [ServerRpc] Attribute. I've tried it without (RequireOwnership = false), I tried removing ServerRpcParam. But as far as I can tell the code inside of TogglePlayerReadyServerRpc is just never even viewed or run though

    Code (CSharp):
    1.  
    2.     private void ClientOnReadyClicked()
    3.     {
    4.         TogglePlayerReady();
    5.         TogglePlayerReadyServerRpc();
    6.         print("This is printing");
    7.     }
    8.  
    9.     public void TogglePlayerReady()
    10.     {
    11.         print("This is printing");
    12.     }
    13.  
    14.     [ServerRpc(RequireOwnership = false)]
    15.     public void TogglePlayerReadyServerRpc(ServerRpcParams serverRpcParams = default)
    16.     {
    17.         print("This is not printing");
    18.     }
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,975
    Are you observing the console on the server or just the client where you click the button? Cause the client won‘t execute a ServerRPC method, it will send a message to the server so that the server executes that method. Just in case you weren‘t expecting that.

    Second thing could be that these methods aren‘t in a class inheriting from NetworkBehaviour.
     
  3. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    229
    Or the script isn't on a gameobject with a spawned NetworkObject component
     
    RunninglVlan likes this.
  4. kemijo

    kemijo

    Joined:
    Apr 7, 2014
    Posts:
    18
    Last edited: Oct 29, 2022
  5. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,975
  6. BurnEmDown

    BurnEmDown

    Joined:
    Dec 29, 2019
    Posts:
    28
    Hi, I'm running into the same issues you've mentioned here (I actually posted a thread about it a few weeks ago:
    https://forum.unity.com/threads/sending-serverrpcparams-on-a-buttons-onclick-method.1348490/ )

    However, my buttons get instantiated and spawned dynamically and are not in-scene. Can you specify where exactly on the page you linked to did you find the answer?

    Technically the buttons aren't spawned and don't have a networkobject on them but the method which is called by onClick is on a parent object which is spawned and does have a networkobject. First there is a non-networked method, which calls a serverRpc method just like you tried. Could it be that I have to get the buttons themselves to have a networkobject component as well?
     
  7. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,975
    Do you call the Rpc method directly from a button event or a script that handles button events? If so, simply add a layer of indirection:

    networkedParent.SendSomeRpcMessage()

    and inside that SendSomeRpcMessage which is on the networked object you‘d simply call the WhateverServerRpc method. That way any network messaging is entirely encapsulated within the network object.
     
  8. BurnEmDown

    BurnEmDown

    Joined:
    Dec 29, 2019
    Posts:
    28
    What I've done so far is that the button calls a non-network method on the networked script, and that method calls the ServerRpc method. I'll quickly give it a try with a different script for the non-networked method.
     
  9. BurnEmDown

    BurnEmDown

    Joined:
    Dec 29, 2019
    Posts:
    28
    This still doesn't seem to be working, I've tried to do something like this:

    void OnClick()
    {
    parent.MethodServerRpc();
    }

    And it still doesn't get called, then I've tried:

    void OnClick()
    {
    Debug.Log("trying to reach ServerRpc");
    parent.Method();
    }

    which in the parent script is:

    public void Method()
    {
    MethodServerRpc();
    }

    But again the ServerRpc doesn't get called, I do see the console log when I click the button.
    Again, the parent is in a spawned NetworkObject. I do see a warning message:

    "[Netcode] Deferred messages were received for a trigger of type OnSpawn with key 0, but that trigger was not received within within 1 second(s)."
    Whenever I click the button though. I've yet to find an explanation for this message.
     
  10. Durium

    Durium

    Joined:
    Feb 1, 2020
    Posts:
    37
    Having the same issue, any ideas? :)
     
  11. Biurk

    Biurk

    Joined:
    Nov 4, 2016
    Posts:
    3
    I had the same issue, it was my NetworkObject who was not Spawned despite being a scene object and thus should be automatically spawned.
    The Reason was that at the start of the NetworkManager, the object was inactivated.
    All of this was done silently, no message that my rpc calls were dropped, no message that I had unspawned NetworkObject, very hard to debug.
     
  12. NoelStephens_Unity

    NoelStephens_Unity

    Unity Technologies

    Joined:
    Feb 12, 2022
    Posts:
    259
    Also, don't forget to make sure you invoke NetworkManager.StartClient on one instance and NetworkManager.StartHost or NetworkManager.StartServer on the other instance. Otherwise, there will be no session started and nothing will be spawned.