Search Unity

Unity Multiplayer Why isnt this variable being updated on other players

Discussion in 'Connected Games' started by UnityUser9860, Feb 18, 2018.

  1. UnityUser9860

    UnityUser9860

    Joined:
    Dec 8, 2015
    Posts:
    179
    I have some chat code but when one player sends a message it only gets updated on their screen, here is my code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.Networking;
    6. using System;
    7.  
    8. public class Chat : NetworkBehaviour {
    9.  
    10.     public string ChatFeed;
    11.     public bool   chatActive;
    12.     [Space]
    13.     public Text           FocusedText;
    14.     public Text           UnFocusedText;
    15.     public InputField     MessageInput;
    16.     public CanvasGroup    ChatCanvasGroup;
    17.     public ConsoleManager Console;
    18.  
    19.     void Start()
    20.     {
    21.         if (PlayerRank.Rank() == "dev")
    22.         {
    23.             SendMSG(">>Developer: " + PlayerTag.GetTag() + " Has Joined<<");
    24.         }
    25.         else
    26.         {
    27.             SendMSG(PlayerTag.GetTag() + " Has Joined!");
    28.         }
    29.         GetRequiredObjects();
    30.     }
    31.  
    32.     void GetRequiredObjects()
    33.     {
    34.         ChatCanvasGroup = GameObject.Find("Chat").GetComponent<CanvasGroup>();
    35.         MessageInput    = GameObject.Find("ChatInput").GetComponent<InputField>();
    36.         UnFocusedText   = GameObject.Find("ChatTextOutside").GetComponent<Text>();
    37.         FocusedText     = GameObject.Find("ChatTextFocused").GetComponent<Text>();
    38.         Console         = GameObject.Find("ConsoleManager").GetComponent<ConsoleManager>();
    39.     }
    40.  
    41.     [Command]
    42.     void Cmd_SendChatMessage(string message)
    43.     {
    44.         ChatFeed += message + "\n"; //Add message to chatfeed with a return
    45.         Rpc_UpdateChat(ChatFeed);
    46.     }
    47.  
    48.     [ClientRpc]
    49.     void Rpc_UpdateChat(string newChat)
    50.     {
    51.         ChatFeed = newChat;
    52.     }
    53.  
    54.     public void SendMSG(string Message)
    55.     {
    56.         Cmd_SendChatMessage(Message);
    57.     }
    58.  
    59.     void ClearInputBox()
    60.     {
    61.         MessageInput.text = ""; //Reset box
    62.     }
    63.  
    64.     void Update()
    65.     {
    66.         if (!isLocalPlayer) //Stop non-clients
    67.         {
    68.             return;
    69.         }
    70.         if (Console.ConsoleInput.IsActive() == true) //If console is active dont do anything
    71.         {
    72.             return;
    73.         }
    74.  
    75.         FocusedText.text   = ChatFeed; //Set focused chat text
    76.         UnFocusedText.text = ChatFeed; //Set unfocuesd chat text
    77.         string message = MessageInput.text; //get input box text
    78.  
    79.         if (Input.GetKeyDown(KeyCode.Return)) //on press enter
    80.         {
    81.             SendMSG(PlayerTag.GetTag() + ": " + message); //Send message
    82.             ClearInputBox(); //clear the input box text
    83.         }      
    84.  
    85.         if (Input.GetKeyDown(KeyCode.T) && MessageInput.isFocused == false) //If user presses T and input box isnt focused
    86.         {
    87.             if (ChatCanvasGroup.alpha == 1) //If chat is visible
    88.             {
    89.                 ChatCanvasGroup.alpha = 0;
    90.                 ChatCanvasGroup.interactable = false;
    91.                 ChatCanvasGroup.blocksRaycasts = false;
    92.                 GetComponent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController>().enabled = true;
    93.                 GetComponent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController>().LockCursor();
    94.                 chatActive = false;
    95.             }
    96.             else
    97.             {
    98.                 ChatCanvasGroup.alpha = 1;
    99.                 ChatCanvasGroup.interactable = true;
    100.                 ChatCanvasGroup.blocksRaycasts = true;
    101.                 GetComponent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController>().UnlockCursor();
    102.                 GetComponent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController>().enabled = false;
    103.                 MessageInput.Select();
    104.                 chatActive = true;
    105.             }
    106.         }
    107.     }
    108. }
    109.  
     
  2. vis2k

    vis2k

    Joined:
    Sep 4, 2015
    Posts:
    3,241
    Rpc is only sent to observers, not to everyone on the server
     
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    6,297
    The way it appears you have this set up is every Player GameObject has their own copy of this Chat script attached. Each Chat script instance has its own unique ChatFeed string that contains the latest updates to the chat. So for that to work, you would need each client to take the contents of all ChatFeed strings on all GameObjects with Chat components, and combine them together somehow to display everyone else's chats.

    But what you're doing in Update is if this !isLocalPlayer you immediately return, so you never process anything in ChatFeed on any GameObject besides the local player. So you're never able to see any text sent by any other player, because any other player's Player GameObject would have isLocalPlayer set to false. Just switching that isn't going to just fix this though.

    What I would do instead of having a separate ChatFeed for every single Player GameObject, I'd come up with a centralized solution where there is a single ChatFeed for the entire game.