Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[Command] call doesn't work from Player GameObject

Discussion in 'Multiplayer' started by Jackdahack, Mar 1, 2019.

  1. Jackdahack

    Jackdahack

    Joined:
    Sep 11, 2018
    Posts:
    9
    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<Canvas>().enabled = false;
    gameCanvas = GameObject.Find("GameCanvas");
    gameCanvas.GetComponent<Canvas>().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<NetworkIdentity>().AssignClientAuthority(this.GetComponent<NetworkIdentity>().connectionToClient);
    }
    }
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    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.
     
  3. Jackdahack

    Jackdahack

    Joined:
    Sep 11, 2018
    Posts:
    9
    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.
     
  4. Jackdahack

    Jackdahack

    Joined:
    Sep 11, 2018
    Posts:
    9
    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?
     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    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():

    Code (csharp):
    1. if (isLocalPlayer == false)
    2. {
    3.     Debug.Log("OnClickStart called on wrong Player GameObject instance");
    4. }
    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.