Search code examples
c#unity-game-engineunity3d-unet

UNET: Command function CmdSpawnPlayer called on server


I got an error from Unity while I was busy executing a command for the server. The error is:

Command function CmdSpawnPlayer called on server.

UnityEngine.Debug:LogError(Object)

NetworkHandler:CallCmdSpawnPlayer(Boolean) NetworkHandler:OnEnable() (at Assets/Scripts/Managers/NetworkHandler.cs:69)

UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()

Here is the code I use:

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

public class NetworkHandler : NetworkBehaviour
{
    public Player NetworkPlayer
    {
        get;
        private set;
    }

    public bool HasInternet
    {
        get
        {
            return Network.HavePublicAddress();
        }
    }

    public bool IsConnectedToServer
    {
        get
        {
            return networkManager.isNetworkActive;
        }
    }

    [SerializeField]
    private NetworkManager networkManager;

    [SerializeField]
    private GameObject playerPrefab, spectatorPrefab;

    public void StartClient()
    {
        networkManager.StartClient();
    }

    public void StartServer()
    {
        networkManager.StartServer();
    }

    [Command]
    private void CmdSpawnPlayer(bool isSpectator)
    {
        SpawnPlayer(isSpectator);
    }

    private void SpawnPlayer(bool isSpectator)
    {
        if (isSpectator)
        {
            NetworkServer.SpawnWithClientAuthority(Instantiate(spectatorPrefab), connectionToClient);
        }
        else
        {
            NetworkServer.SpawnWithClientAuthority(Instantiate(playerPrefab), connectionToClient);
        }
    }

    private void OnEnable()
    {
        if (IsConnectedToServer)
        {
            CmdSpawnPlayer(true);
            Game.CameraHandler.InstantiateMode();
        }
    }
}

The Unity version is use is 5.3.4f1.


Solution

  • Okay, nevermind. It seems that I have called CmdSpawnPlayer(true); from the server instead of the LocalClient.