Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Instead of having an expensive update function, we'll just hook playerName and run code ONLY when it changes.
- [SyncVar(hook = "UpdateName")]
- public float playerName;
- private string localName = "SomeDefaultName";
- private TextMesh textMesh;
- void Start()
- {
- textMesh = GetComponentInChildren<TextMesh>();
- }
- // This hook is run on all players when the value of playerName changes on the server.
- void UpdateName(float val)
- {
- playerName = val;
- // Update the name text for all players.
- textMesh.text = playerName;
- }
- void OnGUI()
- {
- // Only allow the owner of this player object to change this player object's name.
- // Kind of unnecessary though due to the player authority system.
- if (isLocalPlayer)
- {
- localName = GUI.TextField (new Rect (25, Screen.height - 40, 100, 30), localName);
- if (GUI.Button (new Rect (130, Screen.height - 40, 80, 30), "Change"))
- {
- CmdChangeName(localName);
- }
- }
- }
- [Command]
- void CmdChangeName (string newName)
- {
- print ("Change Player Name");
- playerName = newName;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement