- Home /
Network.Instantiate and NetworkServer.Spawn not working
Hi!
I'm trying to create a multiplayer game in Unity. I want to spawn a few cubes when the server starts, but nor Network.Instantiate nor NetworkServer.Spawn is working.
When I try to call Network.Instantiate() the error is "Failed Network.Instantiate because we are not connected."
When I use NetworkServer.Spawn(), the objects do get spawned, but they all have the same network viewID (0). If I try to allocate the viewId manually, I get the error that I should increase the minimum client networkViewId count. I don't have any other objects in the scene, and also I tried setting it to 500, but the error remains.
The script runs Server Only.
The script itself:
int itemsToSpawn = 6;
List<GameObject> itemPrefabs = new List<GameObject>();
void Start()
{
for ( int i = 0; i < itemsToSpawn; i++)
{
Network.Instantiate(itemPrefabs[random], position, location, 1);
}
}
I also tried OnStartServer() instead of Start(), did not change anything. itemPrefabs is a List of prefabs assigned throug the inspector, currently there is only a cube in it.
Also instead of Network.Instantiate, I tried using NetworkServer.Spawn(), like this:
int itemsToSpawn = 6;
List<GameObject> itemPrefabs = new List<GameObject>();
void Start()
{
for ( int i = 0; i < itemsToSpawn; i++)
{
GameObject item = (GameObject)Instantiate(itemPrefabs[random], position, location);
NetworkServer.Spawn(item);
}
}
Thanks for the help.
show us the code, we can't help with so few information
Answer by Orami · Jun 16, 2016 at 12:09 AM
Please note if you are using Unet you need to register prefabs with the Network Manager script. You can find this area by looking under spawn info and there is a list called Registered Spawnable Prefabs. If it says you are not connected you are probably not local hosting or connected to a server which you would need to do to make the NetworkServer.Spawn(go); code work.
I already registered it as a spawnable prefab in the Network $$anonymous$$anager. And I am using the default Network $$anonymous$$anager HUD to start the game as a host, so that couldn't be the issue.
Try adding a network manager HUD script to it (built in)
Answer by Recurring · Feb 08, 2018 at 10:55 AM
I know this is a bit late, but all you need to do is create something like this:
[command]
void Cmd_SpawnItem(){
GameObject item = (GameObject)Instantiate(itemPrefabs[random], position, location);
NetworkServer.Spawn(item);
}
and just call that function from Start().