Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Rasmus_Bonnesen · Jun 12, 2019 at 05:22 PM · c#networkingmultiplayerchat

Multiplayer text Chat, based on Command and ClientRpc not working.

I am currently working on a simple text chat for my multiplayer game. The problem is that the chat message only appears on the client that sends the message, but through my ingame Console, I can see that the Debug.Log in the ClientRpc gets called on every client, so every client gets the message, but only one of the clients (the client that sends the message) gets the message int the chat. Here is my text Chat script: Thanks for your help in advance.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.Networking;
 using TMPro;
 
 
 
 public class SyncChat : NetworkBehaviour
 {
 
     [SerializeField] private TMP_InputField chatInputField;
     [SerializeField] private GameObject chatInputFieldGO;
     [SerializeField] private GameObject chatScrollGO;
     [SerializeField] private ScrollRect chatScrollView;
     [SerializeField] private KillFeed killFeedScript;
     [SerializeField] private GameObject chatContentText;
     [SerializeField] private GameObject chatScrollViewContent;
 
    
      
 
     private float disableMSGTimer;
     [SerializeField] private float msgVisibleTime = 9f;
     string chatContent;
     private Console console;
 
     public bool chatIsOpen = false;
     private string chatMSG;
 
 
     void Start()
     {
         console = GameObject.FindWithTag("Console").GetComponent<Console>();
         chatInputFieldGO.SetActive(false);
         chatScrollGO.SetActive(true);
         chatIsOpen = false;
       
        
     }
 
 
     [Client]
     void Update()
     {
         if (!isLocalPlayer)
         {
             return;
         }
         if (KeyBindingManager.GetKeyDown(KeyAction.enter))
             {
                 if (!console.isOn)
                 {
                     if (!chatIsOpen)
                     {
                         chatInputFieldGO.SetActive(true);
                         chatScrollGO.SetActive(true);
                         chatInputField.ActivateInputField();
                         chatIsOpen = true;
                     }
                     else
                     {
                         if (chatInputField.text != "")
                         {
                             if (chatInputField.text == "/clear")
                             {
                                 SendMsgToChat("<color=green>Chat has been cleared</color>");
                             }
                             if (chatInputField.text != "/clear")
                             {
                                 chatMSG = chatInputField.text;
                                 CmdGetChatMSG(gameObject.GetComponent<Player>().localPlayerName, chatInputField.text);
                                                             
                                
                             }
                         }
                         chatInputField.text = "";
                         chatInputFieldGO.SetActive(false);
                         StopAllCoroutines();
                         StartCoroutine(ScrollDownCO());
 
                         //chatScroll.SetActive(false);
                         chatIsOpen = false;
                     }
                 }
 
             }
         
        
     }
 
 
    
 
     [Command]
     void CmdGetChatMSG(string _sourcePlayer, string _msg)
     {
         RpcSendMsgToAllPlayers("\n ["+_sourcePlayer+"] " +_msg);
     }
 
 
     [ClientRpc]
     void RpcSendMsgToAllPlayers(string _msg)
     {
         chatScrollGO.SetActive(true);
         SendMsgToChat(_msg);
         Debug.Log("[From ClientRPC: " + _msg);
 
 
     }
 
 
     IEnumerator ScrollDownCO()
     {
         yield return new WaitForEndOfFrame();
          chatScrollView.verticalNormalizedPosition = 0f;
         yield return new WaitForSeconds(msgVisibleTime);
         chatScrollGO.SetActive(false);
 
     }
 
     [SerializeField] private int maxMessages = 50;
      List<TextChatMSG> textChatMessageList = new List<TextChatMSG>();
 
     private void SendMsgToChat(string msgText)
     {
         StopAllCoroutines();
         StartCoroutine(ScrollDownCO());
 
 
         if(textChatMessageList.Count >= maxMessages)
         {
             Destroy(textChatMessageList[0].chatContentText.gameObject);
             textChatMessageList.Remove(textChatMessageList[0]);
         }
         TextChatMSG newTextChatMessage = new TextChatMSG();
         newTextChatMessage.msgText = msgText;
         GameObject newTextItem = Instantiate(chatContentText, chatScrollViewContent.transform);
         newTextChatMessage.chatContentText = newTextItem.GetComponent<TextMeshProUGUI>();
         newTextChatMessage.chatContentText.text = newTextChatMessage.msgText;
             
 
         textChatMessageList.Add(newTextChatMessage);
 
 
         chatScrollView.verticalNormalizedPosition = 0f;
 
     }
 
     [System.Serializable]
     public class TextChatMSG
     {
         public string msgText;
         public TextMeshProUGUI chatContentText;
     }
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

206 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity networking tutorial? 6 Answers

Only one player for the host but two for the client, 0 Answers

Inherited network message not sending/receiving correctly 0 Answers

How do i check which object was instantiated last? 1 Answer

Chat system is not working 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges