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

vanshika picture vanshika · Mar 1, 2016 · Viewed 12.3k times · Source

I am trying to implement a multiplayer feature in 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);
}

Answer

Jansen picture Jansen · Mar 3, 2016

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.

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