Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

I'm trying to make a chat for multiplayer, but clients messages don't update for eachother.

Discussion in 'Netcode for GameObjects' started by Walte1, Jan 23, 2022.

  1. Walte1

    Walte1

    Joined:
    May 8, 2021
    Posts:
    3
    When a client sends a message it only updates for itself. I'm thinking that the string being sent to other clients might be empty, but I don't know how to solve it.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5. using Unity.Netcode;
    6.  
    7.  
    8. public class ChatBehaviour : NetworkBehaviour
    9. {
    10.     [SerializeField] private GameObject chatUI;
    11.     [SerializeField] private TMP_Text chatText;
    12.     [SerializeField] private TMP_InputField chatInput;
    13.  
    14.     private void Start()
    15.     {
    16.         if (IsOwner)
    17.         {
    18.             chatUI.SetActive(true);
    19.         }
    20.     }
    21.  
    22.     //Called from the InputField
    23.     public void Send(string message)
    24.     {  
    25.         if (IsClient)
    26.         {
    27.             if (!Input.GetKeyDown(KeyCode.Return) || string.IsNullOrWhiteSpace(message))
    28.             {
    29.                 return;
    30.             }
    31.            
    32.             SendMessageServerRpc(message);
    33.             chatInput.text = string.Empty;
    34.         }
    35.     }
    36.  
    37.     [ServerRpc]
    38.     private void SendMessageServerRpc(string message)
    39.     {
    40.         HandleMessageClientRpc(message);    
    41.     }
    42.  
    43.     [ClientRpc]
    44.     private void HandleMessageClientRpc(string message)
    45.     {
    46.         UpdateChat(message);
    47.     }
    48.    
    49.    
    50.  
    51.     public void UpdateChat(string message)
    52.     {
    53.         chatText.text += $"\n{message}";
    54.     }
    55.  
    56. }
     
  2. Walte1

    Walte1

    Joined:
    May 8, 2021
    Posts:
    3
    It turns out that this only changes the text on the gameObject of the client that sent the message. The other clients can see that change but since its not on their own gameObject but rather only on the sender, their chat does not update.
     
  3. Walte1

    Walte1

    Joined:
    May 8, 2021
    Posts:
    3
    I found a way to fix it buy making sure the clients own script is the one that is referenced.
    Change the UpdateChat to this instead.
    Code (CSharp):
    1. public void UpdateChat(string message)
    2.     {
    3.         NetworkManager.Singleton.LocalClient.PlayerObject.GetComponent<ChatBehaviour>().chatText.text += $"\n{message}";
    4.     }
     
  4. covwalter

    covwalter

    Joined:
    Jan 20, 2021
    Posts:
    3
    thank you :)