- Home /
This question was
closed Aug 04, 2016 at 02:45 AM by
Kadenaa for the following reason:
The question is answered, right answer was accepted
Question by
Kadenaa · Aug 04, 2016 at 05:36 AM ·
networkingrpccommandchatunity multiplayer
[UNet] Command not calling RPC on all Clients?
Heyo everyone,
I am having problems getting messages from the server to all of the clients.
The script is placed on my Player object, which has localPlayerAuthority set to true.
public class Chat : NetworkBehaviour {
List<string> chatLog = new List<string>();
string input = "";
void OnGUI() {
if(isLocalPlayer) {
foreach(string message in chatLog)
GUILayout.Label(message);
input = GUILayout.TextField(input);
if(GUILayout.Button("Send")) {
CmdSendMessageRequest(input);
input = "";
}
}
}
[Command]
void CmdSendMessageRequest(string message) {
RpcRecieveMessage(message);
}
[ClientRpc]
void RpcRecieveMessage(string message) {
chatLog.Add(message);
}
}
Does the command only call the rpc on one client?
If so, how can I get a reference to all of the clients, and by extension to their gameObjects?
If the RPC should be called on all clients, why isn't it working in this instance?
Comment
For those wondering, I here is my fix.
public class Chat : NetworkBehaviour {
public Server$$anonymous$$anager manager;
[Command]
void CmdSend$$anonymous$$essageRequest(string message) {
foreach(GameObject client in manager.clients)
client.GetComponent<Chat>().RpcRecieve$$anonymous$$essage(message);
}
}
public class Server$$anonymous$$anager : Network$$anonymous$$anager {
public List<GameObject> clients = new List<GameObject>();
public override void OnServerAddPlayer(NetworkConnection connection, short playerControllerId) {
base.OnServerAddPlayer(connection, playerControllerId);
clients.Add(connection.playerControllers[0].gameObject);
}
public override void OnServerDisconnect(NetworkConnection connection) {
clients.Remove(connection.playerControllers[0].gameObject);
base.OnServerDisconnect(connection);
}
public override void OnStopServer() {
base.OnStopServer();
clients.Clear();
}
}
Follow this Question
Related Questions
SyncListString not changing in a Command 0 Answers
SyncListString not changing properly 0 Answers
CHAT Multiplayer Problem 1 Answer
RPC Broadcast using Unet 1 Answer
Gameobject wont move through a function to a ClientRPC 0 Answers