- Home /
 
 
               Question by 
               EpicPandaGamer25 · Jan 02, 2016 at 08:19 AM · 
                photonrpceventschat  
              
 
              Photon Unity Networking Chat System Problems...
I am trying to make a chat/message system for my game. The player can enter a chat message and press enter to send the message. The message is sent to everyone, but it will not display properly in the chat gui. Here is Chat.cs:
 using UnityEngine;
 using System.Collections.Generic;
 
 public class Chat : Photon.MonoBehaviour {
     List<string> chatMessages;
 
     int MaxChatMessages = 5;
 
     string input = "";
     public GUISkin skin;
     public static bool ShowChatInput = false;
 
     public Vector2 scrollpos = Vector2.zero;
 
     void OnPhotonPlayerDisconnected (PhotonPlayer player) {
         if (PhotonNetwork.isMasterClient) {
             AddChatMessage ("<color=aqua>" + player.name + "</color>" + " left the game.");
         }
     }
     
     void OnPhotonPlayerConnected (PhotonPlayer player) {
         if (PhotonNetwork.isMasterClient) {
             AddChatMessage ("<color=aqua>" + player.name + "</color>" + " joined the game.");
         }
     }
 
     public void AddChatMessage (string m) {
         gameObject.GetComponent<PhotonView> ().RPC ("AddChatMessageRPC", PhotonTargets.All, m);
     }
 
     [PunRPC]
     void AddChatMessageRPC (string m) {
         chatMessages.Add (m);
 
         while (chatMessages.Count >= MaxChatMessages) {
             chatMessages.RemoveAt(0);
         }
     }
 
     void OnGUI () {
         GUI.skin = skin;
         GUI.skin.settings.cursorColor = Color.black;
 
         Event e = Event.current;
         if (e != null)
         {
             if (e.type == EventType.KeyDown)
             {
                 if (e.keyCode == KeyCode.Return)
                 {
                     input = "<color=aqua>" + PhotonNetwork.playerName + ": </color>" + input;
                     AddChatMessage(input);
                     input = "";
                }
             }
         }
 
         GUILayout.BeginArea (new Rect(5, 5, Screen.width, Screen.height - 10));
         GUILayout.BeginVertical ();
         GUILayout.FlexibleSpace ();
             
         foreach (string msg in chatMessages) {
             GUILayout.Label (msg);
         }
 
         if (ShowChatInput) {
             input = GUILayout.TextField(input, GUILayout.MaxWidth(350));
         }
 
         GUILayout.EndVertical ();
         GUILayout.EndArea ();
     }
 
     void Awake() {
         chatMessages = new List<string>();
     }
 
     void Update () {
         if (Input.GetKeyDown (KeyCode.Y)) {
             if (ShowChatInput == true && photonView.isMine && NetworkManager.UIHidden == false) {
                 ShowChatInput = false;
                 Cursor.visible = false;
                 Cursor.lockState = CursorLockMode.Locked;
                 return;
             }
 
             if (ShowChatInput == false && photonView.isMine && NetworkManager.UIHidden == false) {
                 ShowChatInput = true;
                 Cursor.visible = true;
                 Cursor.lockState = CursorLockMode.None;
                 return;
             }
         }
     }
 }
 
               I am also getting this error when I press enter:
 ArgumentException: Getting control 2's position in a group with only 2 controls when doing KeyDown
 Aborting
 
               The number changes (2) based on how mant times i press enter to send a chat message. Please help me fix these errors, and thank you in advance :)
               Comment
              
 
               
              Answer by tobiass · Jan 08, 2016 at 10:17 AM
There is a working mini-sample for chat via RPCs in the PUN package.
 Check out the "Worker Demo". In the in-game scene, there's chat. 
Your answer