Search Unity

Resolved ServerRpc not calling, not even on the server host

Discussion in 'Netcode for GameObjects' started by a1creator, Feb 21, 2023.

  1. a1creator

    a1creator

    Joined:
    Jan 18, 2021
    Posts:
    24
    I am writing a little chatsystem for my game, and it has worked before with this exact code. My issue started once I connected using relay instead of just using NetworkManager.Singleton.StartHost(). I still use this, but only after I have connected with Relay. This should still not be an issue, as far as I can gather.

    I am dumbfounded. I have searched the whole web and found 5 others with my issue, but their issue was that they were not deriving from NetworkBehaviour or had RequireOwnership = true. I do derive from NB and as you can see below, RequireOwnership = false.

    Code (CSharp):
    1.  
    2.     public void SendChatMessageLocal(string input)
    3.     {
    4.         if (input != string.Empty)
    5.         {
    6.             SendChatMessageServerRpc(username, input);
    7.         }
    8.     }
    9.  
    10.     // we need to turn off RequireOwnership because we are calling code that runs outside of the player object
    11.     [ServerRpc(RequireOwnership = false)]
    12.     public void SendChatMessageServerRpc(string username, string textMessage)
    13.     {
    14.         ReceiveChatMessageClientRpc(username, textMessage);
    15.     }
    16.  
    17.     // make all clients receive it (including sender, thats why we dont do it locally.)
    18.     // sender only sees their message if the server received it!
    19.     [ClientRpc]
    20.     public void ReceiveChatMessageClientRpc(string sender, string message)
    21.     {
    22.         SpawnMessage(sender + ": " + message);
    23.     }
    24.    
    25.     // handle spawning locally
    26.     private void SpawnMessage(string text)
    27.     {
    28.         var obj = Instantiate(messageObjectPrefab, chatParent);
    29.         var msgObj = obj.GetComponent<MessageObject>();
    30.  
    31.         msgObj.SetValues(text);
    32.     }
    I hope to god someone knows what is wrong because I have been stuck for 3 days. Please help
     
  2. a1creator

    a1creator

    Joined:
    Jan 18, 2021
    Posts:
    24
    Solved it but I will leave this up in case someone has the same issue.

    The root of the NetworkBehaviour needs a NetworkObject, not the NB itself. This is why I had issues, because my NB was a chatsystem, which was a child within my UI Canvas.

    I fixed this by splitting my ChatSystem up into two parts;
    - The UI, which is no longer networked in any way
    - A ChatManager object, managing the network logic, with a NetworkObject component

    I hope this helps someone out there, because it took me F***ing ages to realize
     
  3. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    440
    Hi @a1-creator , a little advice: your UI should not be networked at all. This can lead to scaling issues. You should instead synchronize the data that you show in it, and keep your UI locally.
     
    CodeSmile and a1creator like this.
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,005
    Also this feels wrong, to me at least:
    Code (CSharp):
    1. if (input != string.Empty)
    Use this instead:
    Code (CSharp):
    1. if (!string.IsNullOrEmpty(input))
     
  5. a1creator

    a1creator

    Joined:
    Jan 18, 2021
    Posts:
    24
    Hey, thanks for the tip. Is it faster in runtime or the same? I prefer speed over coding convention :)
     
  6. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,005