NetworkServer.Spawn Deleting Objects

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!

    void SpawnAll()
        for (int i = 0; i < 10; i++)
            GameObject obj = Instantiate(obstaclePrefab);


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!

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.

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.

I had a similar issue and in my case, I loaded the new scene with Additive mode.
I would then unload the current scene, causing this problem.

In my case, changing the load scene mode to Single solved this.
If you do need the current scene to be active too and use Additive mode, don’t unload the scene right away. I think NetworkManager does some clean up when it detects scenes being unloaded.