Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

Discussion in 'Netcode for GameObjects' started by neviovalsa, Oct 24, 2023.

  1. neviovalsa

    neviovalsa

    Joined:
    Jun 24, 2019
    Posts:
    33
    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:
    Code (CSharp):
    1. private void SpawnIfNotSpawned()
    2. {
    3.     if(!IsSpawned)
    4.     {
    5.         NetworkObject.Spawn();
    6.     }
    7. }
    I get an error saying the object is already spawned.
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,058
    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.
     
  3. NoelStephens_Unity

    NoelStephens_Unity

    Unity Technologies

    Joined:
    Feb 12, 2022
    Posts:
    244
    @neviovalsa

    Do you have scene management enabled or disabled?
    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:
    upload_2023-10-25_12-53-25.png
    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.
     
  4. neviovalsa

    neviovalsa

    Joined:
    Jun 24, 2019
    Posts:
    33

    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 :)

    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
     
  5. Ed-Gilmour

    Ed-Gilmour

    Joined:
    Dec 14, 2021
    Posts:
    14
    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
    Code (CSharp):
    1. [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.
     
  6. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,058
    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.