- Home /
,How can I resolve the error “Trying to send command for object without authority.”?
I don't know what I did wrong I tried couple different ways from not using commands to using SpawnWithClientAuthority
. But It either gives build and compile errors or just the Error in the title
Here the Start function of my player object
void Start () {
player = isLocalPlayer;
race = Data.races[(int)selectRace].getInstance();
clss = Data.classes[(int)selectClass].getInstance();
health = race.Health * clss.HealthMultiplyer;
speed = race.Speed * clss.SpeedMultiplyer;
armor = race.Armor * clss.ArmorhMultiplayer;
energy = race.Energy * clss.EnergyMultiplayer;
skills.AddRange(race.RaceSkills);
skills.AddRange(clss.ClassSkills);
Data.entities.Add(this);
Debug.Log(isLocalPlayer);
if (!isLocalPlayer)
return;
int enID = Data.entities.IndexOf(this);
foreach (Skill s in skills)
s.CmdSpawn(enID);
}
And here is the spawn functions from skills I try to spawn
[Command]
public override void CmdSpawn(int enID)
{
setEntity(Data.entities[enID]);
Debug.Log("Spawned");
dir = getEntity().lookingAt;
gameObject.transform.position = getEntity().transform.position + dir * 0.65f;
transform.up = dir;
NetworkServer.Spawn(gameObject);
}
and
[Command]
public override void CmdSpawn(int enID)
{
setEntity(Data.entities[enID]);
dir = getEntity().lookingAt;
transform.up = dir;
dir = new Vector3(dir.z, dir.y, -dir.x);
gameObject.transform.position = getEntity().transform.position + dir * 0.65f;
NetworkServer.Spawn(gameObject);
}
If you can help, thank you ^.^
Answer by Diukrone · Jan 16, 2018 at 02:09 AM
You are trying to send command to a object that is not your instantiated prefab ID. So you need to order to give authority and to remove authority after has used it.
//Put authority
assignAuthorityObj.GetComponent<NetworkIdentity>().AssignClientAuthority(this.GetComponent<NetworkIdentity>().connectionToClient);
//Remove authority
assignAuthorityObj.GetComponent<NetworkIdentity>().RemoveClientAuthority(this.GetComponent<NetworkIdentity>().connectionToClient);
Objects must have been spawned with NetworkServer.SpawnWithClientAuthority or have authority set with NetworkIdentity.AssignClientAuthority. Commands sent from these object are run on the server instance of the object, not on the associated player object for the client,
But I am seding it from player Object there is even
if (!isLocalPlayer)
return;
int enID = Data.entities.IndexOf(this);
foreach (Skill s in skills)
s.CmdSpawn(enID);
So even if it wasnt localPlayer It should have not sent it.
!isLocalPlayer = Not Local Player. $$anonymous$$aybe is there your error!
What is a Skill? You call a command on a "Skill" object. If that skill object is not owned by this player you can't send a command to this object. Also this line:
NetworkServer.Spawn(gameObject);
makes no sense. An object can't spawn itself in a command you send from a client. In order to communicate through an object with a NetworkIdentity the object has to be spawned already. I think your spawning command should be inside your Player class. You should pass the prefab of the skill you want to spawn and than let the server spawn the objects from your player object.
However it's difficult to help you out here as network communication can get quite complicated and highly depends on the relationship of the involved objects / prefabs.
Your answer
![](https://koobas.hobune.stream/wayback/20220612150554im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
UNet ClientRPC does not fire 2 Answers
Trouble using [Command] in a login script for a server. 1 Answer
UNET Server not setting [SyncVar] 0 Answers
Can I use Rpc Calls and Commands on the same object? 0 Answers
Trying to send command for object without authority. - change color of object from client 0 Answers