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);
}
}