Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Hey! I'm trying to make a game with a chat and it's not working

Discussion in 'Netcode for GameObjects' started by Bia103, May 28, 2021.

  1. Bia103

    Bia103

    Joined:
    Dec 22, 2020
    Posts:
    2
    Hello! I'm trying to make a chat system in my game with MLAPI. I'm a beginner and I just started learning yesterday. I might have a very dumb question but how should I do it? I mean I tried with the code down below, but it's not working and I don't really know what to do.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5. using MLAPI;
    6. using MLAPI.Messaging;
    7. public class Chat : NetworkBehaviour
    8. {
    9.  
    10.     [SerializeField] private GameObject chatUI = null;
    11.     [SerializeField] private TMP_Text chatText = null;
    12.     [SerializeField] private TMP_InputField inputField = null;
    13.     TMP_Text chatTextMultuplayer;
    14.     string textMessage = "";
    15.  
    16.     void Start()
    17.     {
    18.         //chatUI.SetActive(true);
    19.         chatTextMultuplayer = chatText.GetComponent<TMP_Text>();
    20.  
    21.     }
    22.  
    23.     void Update()
    24.     {
    25.  
    26.     }
    27.    // method called On end edit
    28.     public void ReadString(string s){
    29.         CallChatServerRPC(s);
    30.     }
    31.    
    32.     [ServerRpc(Delivery = RpcDelivery.Unreliable)]
    33.     private void CallChatServerRPC(string s){
    34.         CallChatClientRPC(s);
    35.     }
    36.     [ClientRpc(Delivery = RpcDelivery.Unreliable)]
    37.     private void CallChatClientRPC(string s){
    38.         Debug.Log(inputField.text);
    39.         chatTextMultuplayer.text += inputField.text;
    40.         inputField.text = string.Empty;
    41.     }
    42.            
    43. }
    44.  
     
  2. luke-unity

    luke-unity

    Joined:
    Sep 30, 2020
    Posts:
    306
    Can you describe what's not working? Did you put this script on a GameObject with a NetworkObject component on it?
     
  3. Bia103

    Bia103

    Joined:
    Dec 22, 2020
    Posts:
    2
    The script is in an object whose parent has a NetworkObject component in it. I am trying to design a first-person multiplayer game and to each player I have attached a canvas which holds a text field and an input field. The parent(player prefab) has the network object.

    In the code that I posted the method called On end edit didn't work, I just wrote sentences and nothing happened.

    I kept working yesterday and now I have this code with the following problems:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5. using MLAPI;
    6. using MLAPI.Messaging;
    7. public class Chat : NetworkBehaviour
    8. {
    9.  
    10.     [SerializeField] private GameObject chatUI = null;
    11.     [SerializeField] private TMP_Text chatText = null;
    12.     [SerializeField] private TMP_InputField inputField = null;
    13.     static TMP_Text chatTextMultuplayer;
    14.     string textMessage = "";
    15.  
    16.     void Start()
    17.     {
    18.         //chatUI.SetActive(true);
    19.         chatTextMultuplayer = chatText.GetComponent<TMP_Text>();
    20.  
    21.     }
    22.  
    23.     void Update()
    24.     {
    25.  
    26.     }
    27.    
    28.     public void ReadString(){
    29.         //CallChatServerRPC();
    30.         CallChatClientRPC();
    31.     }
    32.    
    33.     [ServerRpc(RequireOwnership = false)]
    34.     private void CallChatServerRPC(){
    35.  
    36.         //CallChatClientRPC(s);
    37.     }
    38.    [ClientRpc]
    39.     private void CallChatClientRPC(){
    40.         Debug.Log(inputField.text);
    41.         chatTextMultuplayer.text += inputField.text;
    42.        
    43.         inputField.text = string.Empty;
    44.     }
    45.            
    46. }
    47.  
    The problems: Let's say I have two players (player 1 and player 2). Player 1 is the host. Player 1 can write whatever he wants in the chat, only he will see it and player 2 will never receive it.
    Player 2 can write on the input field but the method ReadString which is called On end edit(for input field) will never execute. An interesting thing is that when player 2 writes in the chat something, and player 1 presses enter, player 1 will receive the message from player 2.

    I'm sorry if it's a dumb thing, I never worked with this kind of thing and I'm very confused.
     
  4. RichardWepner

    RichardWepner

    Joined:
    May 29, 2013
    Posts:
    33
    If I'm not mistaken, what you're doing here is:
    - receive the text sent from the other player as parameter `s`
    - discard it (it's not used in the method anywhere)
    - read the client-local content of the text field, which wasn't synchronized through the network, and thus contains exactly what the player with this client entered
    - append this to the chat box

    I didn't try to do anything like this so far, but to me it looks a lot like you'll have to use the value of the parameter `s` instead of trying to read the input from the text field. This might already solve your problem.

    On the other hand, you mentioned that your `ReadString` method isn't called on one of the clients. How did you verify this? (I suggest to add a log message into `ReadString`. And if you're at it: please rename it to something like `SendEnteredTextAsMessage`, `SendMessage`, `SendMessageButtonClicked`, or something similar.) The method not being called indicates that there might _also_ be another issue.