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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Simple chat sync problem

Discussion in 'Multiplayer' started by 3B00DG4MER, Aug 14, 2016.

  1. 3B00DG4MER

    3B00DG4MER

    Joined:
    Jun 24, 2015
    Posts:
    64
    Hi guys,
    I've today to make a little networked chat
    I try to know what's the problem, couldn't know, I'm using the Unity 5 UI and UNET
    here's the code
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. using UnityEngine.Networking;
    5.  
    6. public class PlayerChat : NetworkBehaviour {
    7.  
    8.     [SerializeField]
    9.     private Text chatText;
    10.     [SerializeField]
    11.     private InputField chatInput;
    12.     [SerializeField]
    13.     private Button chatButton;
    14.  
    15.     void OnEnable()
    16.     {
    17.         chatButton.onClick.AddListener(delegate { SendTextChat(); });
    18.         chatInput.onEndEdit.AddListener(delegate { SendTextChat(); });
    19.     }
    20.  
    21.     void SendTextChat()
    22.     {
    23.         Debug.Log(chatInput.text);
    24.         if(chatInput.text != string.Empty)
    25.         {
    26.            string textToSend = transform.name + " : " + chatInput.text;
    27.            CmdSendText(textToSend);
    28.         }
    29.        
    30.     }
    31.  
    32.  
    33.     [Command]
    34.     void CmdSendText(string text)
    35.     {
    36.         RpcSendText(text);
    37.     }
    38.  
    39.     [ClientRpc]
    40.     void RpcSendText(string text)
    41.     {
    42.         chatText.text += text;
    43.         chatText.text += "\n";
    44.         chatInput.text = "";
    45.     }
    46. }
    47.  
    If you want a screenshot of the hierarchy, just tell and i'll attach it ;)
    Help me plz
     
  2. 3B00DG4MER

    3B00DG4MER

    Joined:
    Jun 24, 2015
    Posts:
    64
    I tried to use Debug.Log on text for the ClientRpc void, it works well
    Sometimes i think i should make the chat with the debug log xDD
    Help plz