- Home /
Question by
DrDinesen · Apr 27, 2016 at 05:23 AM ·
errornetworkingspawning problems
Network spawning not working
So i'm building a line wars game, and i have run into a problem where i can instantiate and spawn the object on the local client, and i synchronizes perfectly.. But when i do it on the remote client, it only instantiates and doesn't spawn on the server, i get this error:
SpawnObject for MeleeHouse(Clone) (UnityEngine.GameObject), NetworkServer is not active. Cannot spawn objects without an active server.
I have another object tell the manager to spawn the bulding at it's transform. it looks like this:
using UnityEngine;
using System.Collections;
public class BuldingPlatformScript : MonoBehaviour {
Color defaultColor;
public Color highlightColor;
GameObject graphicGO;
void Start(){
graphicGO = transform.GetChild (0).gameObject;
defaultColor = graphicGO.GetComponent<Renderer> ().material.color;
}
void OnMouseEnter (){
graphicGO.GetComponent<Renderer> ().material.color = highlightColor;
}
void OnMouseExit (){
graphicGO.GetComponent<Renderer> ().material.color = defaultColor;
}
void OnMouseUp () {
GameObject.Find ("_Scripts_").GetComponent<BuildingManager> ().MakeBuilding (transform);
}
}
and the manager script looks like this..
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
public class BuildingManager : NetworkBehaviour {
int currentMoney = 50;
GameObject selectedBuildingGO;
int selectedBuildingPrize = 0;
public bool isMenuOpen = false;
public GameObject menuGO;
int belongsToPlayer = 0;
Transform target;
public void GetPlayerNumber (int playerNumber) {
belongsToPlayer = playerNumber;
}
//Activated by a UI button
public void SetSelectedBuilding (GameObject building){
selectedBuildingGO = building;
}
//Activated by a UI button
public void SetBuildingPrize (int sentPrice){
selectedBuildingPrize = sentPrice;
}
//Activated by a buildingPlatform
public void MakeBuilding (Transform targetPlatform){
if (!isClient)
return;
if (currentMoney >= selectedBuildingPrize) {
target = targetPlatform;
currentMoney -= selectedBuildingPrize;
GameObject building = Instantiate (selectedBuildingGO, target.position, Quaternion.identity)as GameObject;
Destroy(target.gameObject);
building.tag = "Team"+belongsToPlayer+"";
NetworkServer.Spawn(building);
} else {
//DO: show you dont have enough money
}
}
void Update(){
//Check if to open the buildingmenu
if (Input.GetKeyUp (KeyCode.B)) {
if(isMenuOpen == false){
menuGO.SetActive(true);
isMenuOpen = true;
}else{
menuGO.SetActive(false);
isMenuOpen = false;
}
}
if (isMenuOpen == true) {
isMenuOpen = menuGO.activeInHierarchy;
}
}
}
Comment
NetworkServer code should run on server.
if(!isClient) return
is preventing this.