0
\$\begingroup\$

In my game I have an object, let say food, that whoever consumes it, that player will spawn one more element. I am checking collision in Update.

The following code is working fine on server side, but the object is not spawning. Although, it is spawning on local client but it is not updating on server.

Here is my code:

// This function is called on Update
private void CheckForFood(Vector3 snakePartPosToBeInitialize,Vector3 headPos)
{
    if(_food != null)
    {
        if (_food.transform.position == headPos)
        {
            UiControllerCS.UI.showScore();
            GameObject obj = Instantiate(snakePart, snakePartPosToBeInitialize, Quaternion.identity) as GameObject;
            currPartOfSnake += 1;
            obj.name = "" + currPartOfSnake;
            obj.transform.parent = gameObject.transform;
            tail.Add(obj);
            NetworkServer.SpawnWithClientAuthority(obj , connectionToClient);
        }
    }
}

I am getting the following error :

SpawnObject for 1 (UnityEngine.GameObject), NetworkServer is not active. Cannot spawn objects without an active server.

error

\$\endgroup\$
0
\$\begingroup\$

You can try instantiating it on the network using:

Network.Instantiate()

Here is the documentation

\$\endgroup\$
1
  • \$\begingroup\$ Although a good answer, I think it could improve more if together with the link you could provide a small description why/how it works. Remember that in a few years, someone might be looking at your answer, and the link may be down/broken. \$\endgroup\$
    – TomTsagk
    Oct 31 '18 at 11:49
0
\$\begingroup\$

As a client you don't have access to NetworkServer which only exist on the server. If you want to call NetworkServer.SpawnWithClientAuthority you must call it on on the server. You do such via a Remote Procedure Call, in your case a Command.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.