Question by
vanshika1012 · Mar 03, 2016 at 12:38 PM ·
networkingspawnnetworkplayernetwork instantiate
NetworkServer is not active. Cannot spawn objects without an active server.
Hi,
I am trying to implement a multiplayer feature. 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);
}
Comment
Here is my movement Code :-
private void CmdUpdate$$anonymous$$ySnakePos()
{
//try{
if(HeadTransform == null)
return;
Vector3 parentPos = HeadTransform.position;
Vector3 headNextPos = HeadTransform.position + nextStep;
if (headNextPos.x > maxX)
{
headNextPos.x = $$anonymous$$X;
}
else if (headNextPos.x < $$anonymous$$X)
{
headNextPos.x = maxX;
}
else if (headNextPos.y > maxY)
{
headNextPos.y = $$anonymous$$Y;
}
else if (headNextPos.y < $$anonymous$$Y)
{
headNextPos.y = maxY;
}
HeadTransform.position = headNextPos;
int count = gameObject.transform.childCount;
List<GameObject> snakeParts = new List<GameObject>();
for (int i = 0; i < count; i++)
{
GameObject obj = gameObject.transform.GetChild(i).gameObject;
snakeParts.Add(obj);
}
Vector3 lastPos = new Vector3(0,0,0);
Debug.Log("tail count"+tail.Count);
for (int i = 0; i < tail.Count; i++)
{
Vector3 currPos = tail[i].transform.position;
if (i == tail.Count - 1)
{
lastPos = currPos;
}
tail[i].transform.position = parentPos;
parentPos = currPos;
}
CheckForCollision(headNextPos);
CmdCheckForFood(lastPos,HeadTransform.position);
}