[Command] call doesn't work from Player GameObject

The first call of a Command Method at Start() works perfectly fine,
but after that when CmdSpawnPlayer is called i get this error: Trying to send command for object without authority.
My player object has authority all the time and i’m running out of ideas what could cause that problem.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class PlayerConnectionObject : NetworkBehaviour
{
public GameObject playerPrefab;
GameObject player;
GameObject lobbyManager;
GameObject lobbyCanvas;
GameObject gameCanvas;

// Start is called before the first frame update
void Awake()
{
DontDestroyOnLoad(this.gameObject);
}

private void Start()
{
if(isServer)
{
return;
}
CmdAssignAuthorityToClientLobbyManager();
}

// Update is called once per frame
void Update()
{

}

public void OnClickStart()
{
CmdSpawnPlayer();
lobbyCanvas = GameObject.Find(“LobbyCanvas”);
lobbyCanvas.GetComponent().enabled = false;
gameCanvas = GameObject.Find(“GameCanvas”);
gameCanvas.GetComponent().enabled = true;
}

[Command]
public void CmdSpawnPlayer()
{
Debug.Log(“Spawn Player Method called”);

Transform startPosition = NetworkManager.singleton.GetStartPosition();
player = (GameObject)Instantiate(playerPrefab, startPosition.position, startPosition.rotation);

NetworkServer.SpawnWithClientAuthority(player, connectionToClient);
Debug.Log(“Spawn Player”);
}

[Command]
public void CmdAssignAuthorityToClientLobbyManager()
{
lobbyManager = GameObject.Find(“LobbyManager”);
lobbyManager.GetComponent().AssignClientAuthority(this.GetComponent().connectionToClient);
}
}

I’m trying to understand what you’re doing here. You say this is a script on the Player GameObject, but I see this script has references to a player prefab and player GameObject, which this script tries to spawn. Which implies that this script is not on the player GameObject.

The Player Prefab is the playable and visible in game character, but the “Player Object” wich is spawned by the NetworkManager is PlayerConnectionObject .
That’s because i don’t want to directly spawn the visible Player when the client connects to the Server.

I’ve recognized that the function is succesfully called over the Start() function in the same script, but not if i want to call it with OnClickStart().
OnClickStart() is called with a button in the UI.
Is there something i need to do with the Button itself?

Are you sure you are calling OnClickStart() on the Player GameObject which is for that client? If you call it on the wrong object you will get the error you are seeing. You can just add this to OnClickStart():

if (isLocalPlayer == false)
{
    Debug.Log("OnClickStart called on wrong Player GameObject instance");
}

Also, “Player Prefab” and “Player GameObject” have a very specific meaning in Unet. You’re just making it difficult for people who come in here to help you when you use those terms to mean something they aren’t.