Search Unity

Need help with a Chat

Discussion in 'Multiplayer' started by Superl0l, Nov 30, 2015.

  1. Superl0l

    Superl0l

    Joined:
    Nov 23, 2015
    Posts:
    9
    Hello, i'm pretty desperate here because i'm stuck on it for a couple days..
    I want to add a chat system to my game, every tutorial that i've seen or any old threads about it made a Chat byitself, and i just want a Chat for my game, so if i tried to add what they did it didn't work.. So i had to try to make something by myself...
    That's the script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4. using System.Collections.Generic;
    5. using UnityEngine.UI;
    6.  
    7. public class Chat : NetworkBehaviour
    8.  
    9.  
    10. {
    11.  
    12.     [SerializeField]
    13.     private Text _chatText;
    14.     [SerializeField]
    15.     private InputField _sendTextInput;
    16.     [SerializeField]
    17.     private GameObject _playerCanvas;
    18.     [SerializeField]
    19.     private SyncListString chatLog = new SyncListString();
    20.  
    21.     void Start()
    22.     {
    23.         if (isLocalPlayer)
    24.         {
    25.             _playerCanvas.SetActive(true);
    26.         }
    27.         if (!isLocalPlayer)
    28.         {
    29.             _playerCanvas.SetActive(false);
    30.         }
    31.     }
    32.  
    33.     [Command]
    34.  
    35.     void CmdSendMessage()
    36.     {
    37.         ChatMessageMsg msg = new ChatMessageMsg();
    38.         msg.Message = ("Player " + netId + ": " + _sendTextInput.text);
    39.         _sendTextInput.text = string.Empty;
    40.         Debug.Log(ClientScene.FindLocalObject(netId).name);
    41.         RpcReceiveChat(msg.Message);
    42.     }
    43.  
    44.     [ClientRpc]
    45.     public void RpcReceiveChat(string msg)
    46.     {
    47.         chatLog.Add(msg);
    48.         _chatText.text += chatLog[chatLog.Count - 1] + "\n";
    49.     }
    50.  
    51. }
    Basically i have a Player prefab, it has the chat UI in it (So i can use the UI components with the script).
    ChatMessageMsg has 1 string that inheritances the MessageBase class, and there is a Button UI that onClick calls the CmdSendMessage().
    So, when players spawn, each player can control his own character, etc..
    When the host sends a message it sends: "Player 1:"blabla"
    But when a client sends a message it sends: "Player 2:" without the message...
    Also the messages that were sent didn't display on the chat for all the clients.. Just to the client who sent the message ><
    I hope someone could help me with it..
     
    Last edited: Nov 30, 2015