简体   繁体   English

网络服务器未激活。 在 Unity 3D 中没有活动服务器时无法生成对象

[英]NetworkServer is not active. Cannot spawn objects without an active server in Unity 3D

I am trying to implement a multiplayer feature in Unity3d.我正在尝试在 Unity3d 中实现多人游戏功能。 Its like a snake game, snake eats food and generate its part.它就像一个蛇游戏,蛇吃食物并产生它的一部分。 I can successfully generate food and part as well but its not moving and giving me error "NetworkServer is not active. Cannot spawn objects without an active server."我也可以成功生成食物和零件,但它不会移动并给我错误“网络服务器未激活。没有活动服务器无法生成对象。”

Here is my code :-这是我的代码:-

  private void CmdCheckForFood(Vector3 snakePartPosToBeInitialize,Vector3 headPos)
{

    if(_food != null)
    {
        if (_food.transform.position == headPos) // if food collide with head. 
        {
            _food.transform.position = GenerateRandomPosForFood(); 
               currPartOfSnake += 1;
            CmdCreatePartSnake(snakePartPosToBeInitialize);


        }
    }

}
public void CreatePartSnake(Vector3 snakePartPosToBeInitialize)
{
    GameObject obj = Instantiate(snakePart, snakePartPosToBeInitialize, Quaternion.identity) as GameObject;

    obj.name = "" + currPartOfSnake;
    obj.transform.parent = gameObject.transform;
    tail.Add(obj); // tail is list which contain all part of snake
    NetworkServer.Spawn(obj);
}

You're trying to call the NetworkServer on the client side, so in this case CreatePartSnake should be a [COMMAND] in order to run on the Server.您正在尝试在客户端调用 NetworkServer,因此在这种情况下CreatePartSnake应该是 [COMMAND] 以便在服务器上运行。

http://docs.unity3d.com/ScriptReference/Networking.CommandAttribute.html http://docs.unity3d.com/ScriptReference/Networking.CommandAttribute.html

I dont know if you're still having the issue, but as @Jansen said, you need to add the [Command] tag above the function, and also add Cmd to the beginning of the function.我不知道您是否仍然遇到问题,但正如@Jansen 所说,您需要在函数上方添加 [Command] 标记,并在函数的开头添加 Cmd。

Also, you can check to see if the server is active by using NetworkServer.active().此外,您可以使用 NetworkServer.active() 检查服务器是否处于活动状态。

something like if(NetworkServer.active()) should suffice in making sure the function isnt called unless the network server is started.像 if(NetworkServer.active()) 这样的东西应该足以确保除非启动网络服务器,否则不会调用该函数。

Make sure the your server is actually starting as well!确保您的服务器实际上也在启动!

Note: Current unity documentation will make you crazy try this at your own risk注意:当前的统一文档会让您发疯,请自行承担风险

I also face the same problem and i resolve it through making the Function with command Attribute , you should need to change your spawning function something like this我也面临同样的问题,我通过使用命令属性创建函数来解决它,您应该需要像这样更改您的生成函数

[Command]//add command attribute to the spawning function and also add Cmd in function name
    public void CmdCreatePartSnake(Vector3 snakePartPosToBeInitialize)
    {
        GameObject obj = Instantiate(snakePart, snakePartPosToBeInitialize, Quaternion.identity) as GameObject;

        obj.name = "" + currPartOfSnake;
        obj.transform.parent = gameObject.transform;
        tail.Add(obj); // tail is list which contain all part of snake
        NetworkServer.Spawn(obj);
    }

But first you need to derive your class from Network Behaviour .但首先您需要从Network Behavior派生您的类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM