Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Scene objects not synchronized on scene change.

Discussion in 'Multiplayer' started by bteitler, Jul 19, 2015.

  1. bteitler

    bteitler

    Joined:
    May 11, 2014
    Posts:
    52
    I have two scenes. The first, starting scene has my NetworkManager, which is set to "DontDestroyOnLoad". On this scene, all my scene objects synchronize correctly between client and server. However, when I change scene (using Application.LoadLevel, first on server, and then on client via a network message), the scene objects with NetworkBehaviours don't activate (on either server or client). Even if I manually activate them, they do not synchronize.

    I see a few warnings on the client of the type "Did not find target or sync message for".

    If I manually call "NetworkServer.SpawnObjects()" on the server after loading the next scene, the NetworkBehaviours activate on the server, but not on the client, and I get a constant spam of "Did not find target for sync message for: 0" on the client. Manually activating the matching NetworkBehaviours on the client does not fix things after doing this either.

    I also tried using the NetworkManager.ServerChangeScene with similar results. This is somewhat off topic, but that method is inconvenient as it doesn't seem to have a level index version (only takes a scene name.. and there is no way to get scene name by index as far as I can tell). I'm trying to write an API for round robin-ing through all scenes and don't want to hard-code names.
     
    Last edited: Jul 19, 2015
  2. bteitler

    bteitler

    Joined:
    May 11, 2014
    Posts:
    52
    Figured it out I think:

    I had to add "NetworkServer.SetAllClientsNotReady();" to my server before loading the next scene,
    and then:
    "ClientScene.Ready (NetworkManager.singleton.client.connection);" for the client to re-ready after client is done loading.
    Finally, "NetworkServer.SpawnObjects ();" on the server when the server is done loading.

    It isn't clear to me why this was failing before when I never disabled the ready status for the client. Maybe the message(s) to hook up the scene objects were dropped while loading the scene because the servers sent too early? If this is the case, a scene name sanity check of some kind could prevent this or at least give a more verbose warning - seems like an easy mistake to make.
     
    pKallv likes this.
  3. bteitler

    bteitler

    Joined:
    May 11, 2014
    Posts:
    52
    I'm now stumped again though with a new problem:

    1. I start the server app (and start the server), and change scene on the server to a new scene.
    2. I start my client app (not starting a client yet..) and change scene to match the server.
    3. I connect client to server, and then ready the client..

    The client gets "Did not find target for sync message" spam again! Seems like the only way to have scene objects bootstrap correctly when changing scenes is for them to do it lock step together, unless I am missing something again. Maybe the fact that I have some NetworkBehaviours on the initial scene objects with DontDestroyOnLoad is somehow confusing the system... but if that was the case I would expect the other objects in the new scene to connect at least, but they don't.

    I'm not sure I fully understand how things work I guess, as I wouldn't expect the initial scene to sync correctly when the client connects if disconnecting causes a problem. This smells like a bug to me. In the old system this was solved mostly with level prefixing for NetworkViews IDs, but I don't know the equivalent in UNET. Using Unity 5.1.1p3..
     
  4. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,266
    I'm relieved to see this post because I've been frustrated by the behavior of scene objects after a level load for several days.

    I don't get the same error messages you do when I load a new scene using NetworkManager.ServerChangeScene, but I am also not using SyncVars at this stage, and only my Manager is DontDestroyOnLoad. However, I am sending an RPC from OnServerAddPlayer to a scene object after calling NetworkServer.SpawnObjects. OnServerAddPlayer is called automatically after loading each scene (the old player object is destroyed in the scene change). The problem is this RPC never arrives—it just disappears without an error or anything. However, I can manually send the same RPC after the scene is finished loading and it will arrive and work as expected.

    Because of this, I don't really get how to communicate with client scene objects in the scene initialization stage (OnLevelWasLoaded and Awake).
     
  5. bittarello

    bittarello

    Joined:
    Aug 8, 2013
    Posts:
    8
    I just here to thank bteitler. This part helped me a lot!
     
  6. Fatbars

    Fatbars

    Joined:
    Jun 14, 2016
    Posts:
    3
    Hello
    Is there a video tutorial regarding syncing scene change from the host to the client? I am a newbie and trying to use network lobby to create a guided VR scene experience by changing to different scenes. Got it to work on the host. But the clients did not sync. Any help appreciated
     
  7. GDesmoulins

    GDesmoulins

    Joined:
    Apr 26, 2013
    Posts:
    7
    Hello everyone,
    For my part, i had sync problems on scene change to. When client connect / disconnect / reconnect ... network objects where spawned randomly, or not ... I didn't use Online & Offline scene for this result. Only "ServerChangeScene"

    I triyed many things, and i managed to make it works :
    using offline & online scene .... and the exact "ServerChangeScene" scripts i used ...

    so, try to force use an offline scene (initialisation scene with networkManager), an Online scene (a lobby, or game logo/menu), then, use "ServerChangeScene" on server.

    Beware ! dont use exact scene for Offline & Online scene, but differents !

    Hope it will help someone !