Hey guys,
I just started out with the Unity Networking System. I’m on version 5.3.4f1 of Unity and all I’m trying to is get a simple Command function working.
I got this from a tutorial that showed the code working fine, and I have all of Network Components in place from Network Managers that spawn the player that is calling this code to Network Identity on said player set to Local Player Authority.
All I’m trying to do with this code is set a Mesh Text to what the player types into a GUI text field. Here is the code:
[SyncVar]
public string playerName = "userName";
void OnGUI()
{
if (isLocalPlayer)
{
playerName = GUI.TextField (new Rect (25, Screen.height - 40, 100, 30), playerName);
if (GUI.Button (new Rect (130, Screen.height - 40, 80, 30), "Change"))
{
CmdChangeName (playerName);
}
}
}
[Command]
void CmdChangeName (string newName)
{
print ("Change Player Name");
playerName = newName;
}
void Update()
{
if (isLocalPlayer)
{
//Sets Player Name.
this.GetComponentInChildren<TextMesh> ().text = playerName;
}
}
When I run the code as the Host, I can clearly see that my “print(“Change Player Name”);” call is getting called just fine, but it does not get called as the client. And neither the host or the client can see any changes of the Text Mesh from each other.
The code is of course using the UnityEngine.Networking, and is in NetworkBehaviour. I simply can not understand what would cause this not to work, so any help with this is greatly appreciated.
Thanks ahead of time!
- Joe