UNET:在服务器上调用的命令函数 CmdSpawnPlayer

UNET: Command function CmdSpawnPlayer called on server

我在忙于为服务器执行命令时从 Unity 收到错误。错误是:

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()

特此我使用的代码:

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

Unity使用的版本是5.3.4f1。

希望你能帮我解决这个问题

Skittluier

好的,没关系。看来我调用了 CmdSpawnPlayer(true);从服务器而不是 LocalClient。