How do I resolve the error “Trying to send command for object without authority.”?

How do 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 ^.^

Solutions/Answers:

Answer 1:

Well this is the problem which i usally get when first i were learning UNET. So here is the general answer and i am sure it will solve your problem once you get that How UNET actually work.

Beside your code fragment, the error

Trying to send command for object without authority.

clearly means that you are sending Command request from an object whose authority you don’t have.

So What it means and how to resolve it you can see my answer Here.

According to your code fragment, Before sending the command (or executing command function), Assign authority of that object(where your script attached) to your Player.

hope that it will make sense and solve the problem. Happy!

Answer 2:

Your problem here is this line:

s.CmdSpawn(enID);

You are performing a Command on your Skills from the PlayerController which your player doesn’t seem to have authority over. There are a few ways to fix this. Either you can assign authority of the skills to the player something like:

s.GetComponent<NetworkIdentity>().AssignClientAuthority(connectionToClient);

OR

I would personally change around your Spawn method so that it is on your PlayerController and instead of just passing in the enID, you should pass in the Skill s and the enID

CmdSpawn(s, enID);

References