- Home /
NetworkServer.Spawn Deleting Objects
Hello, I have looked around many places to try and find the answer to this problem, and it appears that no one else is seeming to have this particular trouble.
I am trying to spawn some objects on the server (which the clients should see just appear on their screen).
I know that I need NetworkServer.Spawn(GameObject go) to do this, and it works (meaning it spawns the object).
However, 1 frame later they are all gone and I have no idea why!
[Server]
void SpawnAll()
{
for (int i = 0; i < 10; i++)
{
Debug.Log("Spawning...");
GameObject obj = Instantiate(obstaclePrefab);
NetworkServer.Spawn(obj);
}
Destroy(this.gameObject);
}
When the level is loaded, that is called. It spawns the 10 objects, but then they are immediately destroyed or deleted or something happens to them. There are no errors that are occuring in the console, they just disappear.
If I run the server as a dedicated server the objects will persist (sort of_. Connecting a client to that LAN Server will then spawn errors (Failed to spawn server object).
I tried adding the prefab to the spawnable objects on the network manager, I tried registering the prefab via code, I tried loading the prefab in via code, the prefab DOES have a network identity (and a network transform). I tried turning on local player authority, server only, both, and none.
I'm using the default network manager (mostly) for now, with the default network manager HUD. When I click Start Host (H) if I quickly pause unity as fast as possible (ctrl+shift+p) I see this:
Yay cubes!
When I click the next frame button (Not unpause, just next frame) I see this (Good bye cubes T_T):
I have no inkling of why this is happening (note it happens with or without the Destroy(this.gameObject), or even if I just spawn 1 object instead of 10 in the loop).
Any help at all would be greatly appreciated!
Answer by Dibbie · Nov 09, 2015 at 10:30 PM
This is how your script is working...
When SpawnAll is called, it will run through your for loop, spawn your stuff
So now, once your for loop is done... You want it to delete your object
Now, if its something thats only called once (like in Start or something), then really, only the last object should be deleted. If its something thats constantly being called (like in Update), then your probably going to experience something like that. Also, if you have this script on your prefab your spawning, its going to spawn the prefab, then delete itself, but every prefab has the same script, so their also going to delete themselves.
Im actually not too sure why youd want to delete one object after you want to spawn a bunch of objects, so try making that delete more specific, like delete only if theres more then 9 of the same object/tag in the scene or something.
The script is on a throwaway script, just meant to spawn in the 10 objects and then never again do anything. This script is not on the prefab, its on an empty game object just hanging out in the scene.
[Server]
void SpawnAll()
{
for (int i = 0; i < 10; i++)
{
Debug.Log("Spawning...");
GameObject obj = Instantiate(obstaclePrefab);
NetworkServer.Spawn(obj);
}
//Destroy(this.gameObject);
}
Produces the same result (the objects are there and then destroyed). I really appreciate your help though!!
Answer by seanr · Nov 10, 2015 at 02:08 PM
probably the scene is still changing. The engine thinks those objects are in the old scene, so it destroys them.
wait until the new scene is fully loaded.