In scene placed network objects do not spawn if in the scene before starting host

This doesn’t happen on the first Play when starting the editor, but keeps happening after, even with a domain reload.

Present in Netcode 1.5, 1.6, 1.7

  • The network prefab instance is at the top of the hierarchy
  • It has a network object component attached
  • It is added to the networks prefabs list
  • It’s a NetworkBehabviour

Note that if I call a function with body:

private void SpawnIfNotSpawned()
{
    if(!IsSpawned)
    {
        NetworkObject.Spawn();
    }
}

I get an error saying the object is already spawned.

Hmmm … I can only remember that the first thing I did was to load a scene with NetworkManager’s scene manager after starting a server or host.

Scanning over the SceneManagement page seems telling. Basically, only scenes loaded with NetworkSceneManager synchronize NetworkObjects with clients. If you do not change scenes I would expect the behaviour you are seeing since you seem to not change scenes after starting the host, which means the scene you are currently in has been loaded “offline” and thus will not synchronize.

@neviovalsa

Do you have scene management enabled or disabled?
9430994--1322219--upload_2023-10-25_12-38-41.png
If enabled, you don’t need to add in-scene placed network prefab instances to the prefab list (although adding them wouldn’t cause any negative side effects either).

However, if you have disabled scene management then you most likely will need to create a prefab instance override per in-scene placed prefab instance:


However, there is a known issue with this that does have a pending fix where under the scenario that you want to create multiple in-scene placed instances of the same network prefab the overrides (one for each instance) will cause an exception to occur.

Next Steps
If you do have scene management enabled, then if you could provide a replication project it would help and if you are not able to then I can post a generic stripped down in-scene placed NetworkObject example project that you can review and modify to match your scenario (on my end, with scene management enabled, this issue cannot be replicated).

If you do not have scene management enabled and before discussing other potential approaches, could describe what you could not accomplish with scene management enabled (which does include support for handling the spawning of in-scene placed network prefab instances) as there might be a feature in NGO’s scene management that would meet your project’s scene management needs.

Hi,
sorry it’s been a while and I still don’t have a real answer.
I just wanted to say that yes, scene management is enabled and unfortunately I wasn’t able to create a minimal repro as the project is getting bigger and - in all honesty - the code is not the cleanest :slight_smile:

In fact, I’m now in the process of simplifying the code base and refactoring a lot of the architecture, I’ll update this answer should I be able to single this one out.

Thanks for the detailed answer anyway

Any updates on this? After disconnecting the host with shutdown and getting a new networking manager in the original scene, creating a new allocation and then getting people to join it when I send RPCs through my scene objects I get [Netcode] Deferred messages were received for a trigger of type OnSpawn with key 3, but that trigger was not received within within 1 second(s). And spawning the objects doesn’t do anything. This only occurs after the first allocation the host makes.

Why is that? The NetworkManager is a singleton. It is put in DontDestroyOnLoad. It should always remain the same instance.

In the past people have been getting duplicates and rather than analyzing the issue and fixing it, they just destroyed one instance. Whereas the actual issue could be easily solved by putting the NetworkManager object in a scene that gets loaded before any other networked scene, typically an initialization scene (the very first one) which then does nothing but load the next scene.