Yet again, another networking noob!
Anyways,
I am testing a simple fps networking game.
This script (PlayerSync.cs) is attached to the Player Object that is assigned in the Network Manager (I am using Network Manager and Network Manager HUD)
Its job is to send the player’s name, assigned in a script before connecting to a server, to the server, who then adds the name to the Master Player List (modified on server only) and assigns the new value to a syncvar PlayerList.
PlayerList is then printed out in an OnGUI event.
Top - client #1
Middle - client #2
Bottom - host
PlayerSync.cs
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
public class PlayerSync : NetworkBehaviour {
[SyncVar]
private string playerList;
void Start () {
if (isLocalPlayer)
CmdSetPlayer(Game.name);
}
[Command]
void CmdSetPlayer(string name)
{
Game.masterList +="\n"+ name;
playerList = Game.masterList;
}
void OnGUI()
{
if (isLocalPlayer){
GUILayout.BeginArea(new Rect(Screen.width - 300, 10, 250, 500));
GUILayout.BeginVertical("box");
GUILayout.Label("~Players~");
GUILayout.Space(10);
GUILayout.Label(playerList);
GUILayout.EndVertical();
GUILayout.EndArea();
}
}
}
Game.cs
public static class Game {
public static string masterList = "";
public static void Reset(){
masterList = "";
//only runs on start of scene
}
}
It seems the other clients fail to recieve an incoming clients name, while the incoming client reads all concurrent name.
As far as I know, commands are only executed on the Server, and therefore, MasterList should only be modified/altered on the Host.
I also know that SyncVar can only be edited on the server through a command, and is then (hopefully) synchronized with every client.
Any reasons why this is not working?