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.

Unity Multiplayer 5.1 Chat almost works... Object reference

Discussion in 'Multiplayer' started by FoxCastz, Jun 20, 2015.

  1. FoxCastz

    FoxCastz

    Joined:
    Jun 18, 2015
    Posts:
    75
    Attempting to figure out the new [ClientRpc] system. I got it to work properly on my Movement animation scripts but it wants nothing to do with my chat scripts. Here is what I have:

    public List<string> chatHistory = new List<string>();
    private string msg = "";
    private string username = "Username here";
    private bool submit;

    private void OnGUI(){
    if (submit) {
    Bottom ();
    }
    private void Bottom(){
    msg = GUI.TextField(chatBox, msg);//This makes messages appear above chat input bar
    if (GUI.Button (sendButton, "Send")) {
    msg = username + ": " + msg;
    SendMessage();
    }

    GUILayout.Space (250);
    for (int i = chatHistory.Count - 1; i >= 0; i--) {
    GUILayout.Label (chatHistory);
    }
    }
    private void SendMessage(){
    if(!string.IsNullOrEmpty(msg.Trim())){
    //GetComponent<NetworkView>().RPC("ChatMessage", RPCMode.AllBuffered, new object[]{msg});
    RpcChatMessage(msg);
    msg = "";
    }

    [ClientRpc]//Remote Procedure Call
    public void RpcChatMessage(string message){
    chatHistory.Add(message);
    }
    }