How do I use an InputField to change a player object's variable?
When a player connects to a server, the server spawns a player object with:
A Network Identity (Local Player Authority = true)
Network Lobby Player
Player Identity Script
This is my Player Identity Script:
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class PlayerIdentity : NetworkBehaviour {
[SyncVar] public string screenname;
void Start() {
CmdChangeScreenname ("New Player");
}
[Command]
public void CmdChangeScreenname (string newName) {
string oldName = this.screenname;
if (oldName.Trim () == "") {
this.screenname = newName;
Debug.Log (this.screenname + " has entered the room.");
return;
}
this.screenname = newName.Trim ();
Debug.Log (oldName + " is now known as " + this.screenname);
}
}
When the player object spawns, it changes the screenname variable to "New Player" and logs that this new player has entered the room - as expected.
I want to allow the user to change their screenname themselves, so I created an text Input Field. I've set it so that when the user is done typing (On End Edit), it calls the CmdChangeChangeScreenname() function in the player object script, passing the Dynamic String as an argument.
But when I test this out, the console tells me:
Trying to send command for object without authority.
UnityEngine.Networking.NetworkBehaviour:SendCommandInternal(NetworkWriter, Int32, String)
PlayerIdentity:CallCmdChangeScreenname(String)
UnityEngine.EventSystems.EventSystem:Update()
What am I doing wrong? How can I allow the text box to change the local player's displayname on the server?
Answer by TheFlyingBastard · Apr 30, 2016 at 09:16 AM
Turns out that the button did not call CmdChangeScreenname(). It called the function CallCmdChangeScreenname(). I have no idea what it is, but when I switched it to CmdChangeScreenname, the username changed just fine.
At least... until another player joined... off to tackle the next problem!
Your answer
Follow this Question
Related Questions
i just can't seem to figure out how to make a voting system, using SyncVar hooks and commands 0 Answers
i just can't seem to figure out how to make a voting system, using SyncVar hooks and commands 0 Answers
How to call [Command] from client? 0 Answers
[Command] giving error: Trying to send command for object without authority 1 Answer