Search Unity

  1. Are you interested in providing feedback directly to Unity teams? Sign up to become a member of Unity Pulse, our new product feedback and research community.
    Dismiss Notice

Chat box is not updating when using Unity UI

Discussion in 'Multiplayer' started by pus2meong, Sep 9, 2021.

  1. pus2meong

    pus2meong

    Joined:
    May 3, 2012
    Posts:
    69
    Hello, I'm currently learning MLAPI and get stuck on chat box that not updating when using Unity UI.

    The Chat tutorial is based on the old MLAPI package, and since I use Unity 2019 I make the some change so it will run in Unity 2019.

    The project that I use is the one that came from Unity network tutorial (Golden Path One). In this tutorial I already convert the OnGUI codes into UI, and it work fine.

    Then I add the chat codes inside HelloWorldPlayer.cs, build, and run it. And it work, as you can see below.
    upload_2021-9-9_20-25-43.png

    As you can see, this tutorial is using Unity GUI. I tried to print the chat text in a Text component (Unity UI), it also work, as you can see the black text on the lower right side.

    Code (CSharp):
    1.  
    2.         public NetworkList<string> chatMessages = new NetworkList<string>(new NetworkVariableSettings() {
    3.             ReadPermission = NetworkVariablePermission.Everyone,
    4.             WritePermission = NetworkVariablePermission.Everyone,
    5.             SendTickrate = 5
    6.         }, new List<string>());
    7.  
    8.         private string textField = "";
    9.  
    10.         private void OnGUI() {
    11.             int sz = 40;
    12.             GUI.skin.label.fontSize = GUI.skin.box.fontSize = GUI.skin.button.fontSize = GUI.skin.textField.fontSize = sz;
    13.  
    14.             if (IsClient) {
    15.                 bool enterPressed = Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Return;
    16.                 textField = GUILayout.TextField(textField, GUILayout.Width(Screen.width));
    17.  
    18.                 if ((enterPressed || GUILayout.Button("Send")) && !string.IsNullOrEmpty(textField)) {
    19.                     chatMessages.Add(textField);
    20.                     textField = "";
    21.                 }
    22.  
    23.                 for (int i = chatMessages.Count - 1; i >= 0; i--) {
    24.                     GUILayout.Label(chatMessages[i]);
    25.                 }
    26.                 //How to deal with this problem?
    27.                 chatList = string.Join("\r\n", chatMessages);
    28.                 myChatText.text = chatList;
    29.                 //Debug.Log("ChatSend Call");
    30.             }
    31.         }
    32.  

    So then I decided to convert the chat input field and button, from GUI to UI. And strange thing happen.
    upload_2021-9-9_20-41-23.png

    I type three lines of text from the stand alone executable on the right side:
    Hello sir
    My I know your name?
    My name is randi

    But this chat did not enter the Text UI.
    I'm sure if the NetworkList is being updated because, when I typing something in Unity Editor, Debug Low is showing the 3 lines of text that being sent from the stand alone executable.
    upload_2021-9-9_20-45-19.png
    This reply will not visible to the right side. But I have tested that all chat message is being updated, but cannot appear in the Text UI.

    This is the UI code that I use. This function being assigned to the UI Button.
    Code (CSharp):
    1.  
    2.         string chatList;
    3.         void SendChatButtonClick() {
    4.             if (IsClient) {
    5.                 chatMessages.Add(chatInput.text);
    6.                 chatList = string.Join("\r\n", chatMessages);
    7.                 chatInput.text = "";
    8.                 myChatText.text = chatList;
    9.                 Debug.Log("Chat Data:  " + chatList);
    10.             }
    11.         }
    12.  
    Am I missing something here? Or GUI code just simply not working on UI?
     
unityunity