Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved NetworkObject synchronized in the wrong scene

Discussion in 'Netcode for GameObjects' started by Kawottex, Sep 21, 2023.

  1. Kawottex

    Kawottex

    Joined:
    Feb 23, 2019
    Posts:
    11
    Hello,
    I've got an issue with network prefab synchronization with additive scenes. Here's my workflow:
    - My game start with a Scene_boot that contains my NetworkManager
    - Then, a Scene_Network_Menu is loaded in Additive mode. This scene allows the user to be a host or a client. When the user is connected to network, the scene is unloaded. (I'm not using the NetworkManager.SceneManagar on that part).
    - Then, a Scene_Menu is loaded using the NetworkManager.SceneManager, in Additive mode.

    Thus, I've got Scene_boot as main scene and Scene_menu as additive (+ DontDestroyOnLoad which contains the NetworkManager)

    If the host is in the Scene_Menu and spawn a NetworkPrefab while a client is here, I've got no issue, the NetworkPrefab is correctly spawn in the Scene_Menu on both side.
    But, if the host spawn that NetworkPrefab and then the client connects after, the NetworkPrefab will be synchronized in the Scene_boot and not in the Scene_Menu.

    Does someone have any idea on what I'm doing wrong in my workflow ?
    I've tried to use VerifySceneBeforeLoading to invalidate the Scene_boot (thinking that maybe avoiding synchronization like that could do something), but it didn't work.

    Thank you,
    Kevin
     
    Last edited: Sep 21, 2023
  2. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    676
    I've not messed with additive scenes so I thought I'd try replicate what you're doing and I'm struggling to see the benefit of using additive scenes in this way. How are you setting Scene_Menu as the active scene, with SceneManager.SetActiveScene?
     
  3. Kawottex

    Kawottex

    Joined:
    Feb 23, 2019
    Posts:
    11
    Hi @cerestorm, thank you for your help.
    On my first tests, the active scene was Scene_boot. I've tried to set Scene_Menu as the active scene, but it didn't change anything on my spawning issue (network prefab still spawned in scene_boot)

    I could probably avoid using additive scenes on that case, but I wanted to understand what's happening, specially if it happens to me again in a case scenario where I can't avoid using additive scenes.
     
  4. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    676
    I ask in reference to this:
    I find the spawn happens in Scene_boot as it remains the active scene after loading Scene_Menu. I can use SceneManager.SetActiveScene to switch it but that's not synchronised and there's no counterpart in NetworkSceneManager to do the same unless I'm missing something. Are you doing something differently to get the spawns in Scene_Menu?
     
  5. Kawottex

    Kawottex

    Joined:
    Feb 23, 2019
    Posts:
    11
    Oh I see, indeed when I do the Instantiate, I specify a transform parent which is located in the Scene_Menu (sorry, I didn't mention it earlier). That's why the prefab is instantiated in that scene when in local, but it seems that the network synchronization doesn't follow
     
  6. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    676
    Are you using NetworkObject.TrySetParent as this appears to do what you're after, at least it does if the parent is an in-scene network object in Scene_Menu.
     
  7. Kawottex

    Kawottex

    Joined:
    Feb 23, 2019
    Posts:
    11
    Hi, sorry for the delay
    Thank you very much, that's what was missing !
    I'm now using TrySetParent after the Spawn and it's correctly shared, thank you again
     
    cerestorm likes this.