- Home /
Trouble using [Command] in a login script for a server.
I have been writing some code to allow players to login or create new accounts for my game. So as always I make some framework and keep checking to make sure it is working correctly. I have run into a snag where a non-local player can not use the commands in this script and it gives the error "Trying to send command for object without authority UnityEngine.Networking.NetworkBehaviour:SendCommandInternal(NetworkWriter, Int32, String)"
Perhaps I am misunderstanding the [Command] deal I thought that was code that was only executed on the server and I could send one parameter? Where code that is executed on the client only is [Client] or [ClientCallback]? Why does it not let me send the login information and gives me an error instead?
I have network identity on the object and set as local player auth
the login script just has a couple InputFields for player input and the rest was just checking to make sure I could decode the name / password.
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
using UnityEngine.UI;
public class Login : NetworkBehaviour
{
public InputField PasswordField;
public InputField UsernameField;
public string username;
public string password;
public string LoginString;
public string usernamedec;
public string passworddec;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
password = PasswordField.text;
username = UsernameField.text;
LoginString = username + "@" + password;
//usernamedec = LoginString.Remove(LoginString.IndexOf('@'));
//passworddec = LoginString.Substring(LoginString.IndexOf('@')+1);
}
public void tryLogin()
{
if (username == "" || password == "") return; //no null fields.
CmdLoginCharacter(LoginString);
}
public void tryCreate()
{
if (username == "" || password == "") return; //no null fields.
CmdCreateNewCharacter(LoginString);
}
[Command]
void CmdCreateNewCharacter(string loginString)
{
Debug.Log("Creating...");
Debug.Log("Name: " + loginString.Remove(loginString.IndexOf('@')));
Debug.Log("Password: " + loginString.Substring(loginString.IndexOf('@') + 1));
//check with PlayerManager to see if the player already exists
//if it does not exist create it and add the player to the server.
createOK();
//if it does exist call createFail() on the client to give an error and ask them to
//use a different name
}
[Command]
void CmdLoginCharacter(string loginString)
{
Debug.Log("Logging in...");
Debug.Log("Name: " + loginString.Remove(loginString.IndexOf('@')));
Debug.Log("Password: " + loginString.Substring(loginString.IndexOf('@') + 1));
//check with PlayerManager to see if the player exists
//if it does not exist call loginFail()
loginOK();
//if a character does exist then check the password to see if it matches if so addplayer and load
//player's existing data.
//if password is wrong then call loginFail()
}
[ClientCallback]
void createOK()
{
ClientScene.AddPlayer(0);
gameObject.GetComponent<Canvas>().enabled = false;
//fields seem to keep focus after turning the canvas off so just turn them off
UsernameField.enabled = false;
PasswordField.enabled = false;
}
[ClientCallback]
void loginOK()
{
ClientScene.AddPlayer(0);
gameObject.GetComponent<Canvas>().enabled = false;
//fields seem to keep focus after turning the canvas off so just turn them off
UsernameField.enabled = false;
PasswordField.enabled = false;
}
[ClientCallback]
void createFail()
{
//say username is already being used.
}
[ClientCallback]
void loginFail()
{
//say username or password is incorrect.
}
}
Have you ever figured out the problem? I'm trying to use an InputField's onEndEdit to call a [command] function in a playerscript which changes a "string playerName" variable in the same class, but it gives me that "Trying to send command for object without authority" error too.
It's weird because when the playerobject (which contains the script) is spawned (going through Start()), it uses that exact same function to change the same variable. It's like the server won't swallow it because it's the InputField that calls that rename function.
Your answer
Follow this Question
Related Questions
,How can I resolve the error “Trying to send command for object without authority.”? 1 Answer
[Networking]How to call a command function from a UI element(a button) 2 Answers
UNET OnTriggerEnter called due to placement of Network-GameObjects on Clients 0 Answers
UNet SyncVar not working 3 Answers
Unity.Networking (UNet) RPC to Specific Player? Master Server? 4 Answers