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

OnServerSceneChanged() not being called

Discussion in 'Multiplayer' started by eikokuma, Nov 17, 2015.

  1. eikokuma

    eikokuma

    Joined:
    Mar 13, 2015
    Posts:
    4
    I'm working on a networked project and I'm having an issue where I call ServerChangeScene() but OnServerSceneChanged() is not being called in response. Everything else, seems to be working perfectly- I can see the scene change happen on both clients. Both clients are set to NotReady() in response to the scene change, and I use OnLevelWasLoaded() to set them back to Ready. The server registers that both clients have been readied again.

    My question is this: what conditions must be true for OnServerSceneChanged() to be called? It must be more then just "the level is completely loaded" because the level is getting completely loaded on every client and nothing is happening. What am I missing?
     
  2. chenyuchih

    chenyuchih

    Joined:
    Jun 3, 2015
    Posts:
    37
    Only server will invoke this callback if you extend NetworkManager
    Code (CSharp):
    1.  
    2.         private void FinishLoadScene()
    3.         {
    4.             if (this.client != null)
    5.             {
    6.                 if (NetworkManager.s_ClientReadyConnection != null)
    7.                 {
    8.                     this.OnClientConnect(NetworkManager.s_ClientReadyConnection);
    9.                     NetworkManager.s_ClientReadyConnection = null;
    10.                 }
    11.             }
    12.             else if (LogFilter.logDev)
    13.             {
    14.                 Debug.Log("FinishLoadScene client is null");
    15.             }
    16.             if (NetworkServer.active)
    17.             {
    18.                 NetworkServer.SpawnObjects();
    19.                 this.OnServerSceneChanged(NetworkManager.networkSceneName);
    20.             }
    21.             if (this.IsClientConnected() && this.client != null)
    22.             {
    23.                 this.RegisterClientMessages(this.client);
    24.                 this.OnClientSceneChanged(this.client.connection);
    25.             }
    26.         }
     
  3. eikokuma

    eikokuma

    Joined:
    Mar 13, 2015
    Posts:
    4
    Hi chenyuchih, thank you for your reply! All of my code is in a script that does extend NetworkManager, so that shouldn't be a problem. I also check to make sure NetworkServer.active is true right before calling ServerChangeScene(), so it should be calling OnServerSceneChanged().

    I was able to write code to manually call OnServerSceneChanged, but I'd like to know why it no longer happens automatically.
     
  4. chenyuchih

    chenyuchih

    Joined:
    Jun 3, 2015
    Posts:
    37
    Hi Eihiko.
    OnServerSceneChanged is a callback of NetworkManager not a callback of NetworkBehavior or Monobehavior. If your OnServerSceneChanged is in a script without extending NetworkManager, you may need to call it from "NetworkManager.OnServerSceneChanged" by yourself
     
  5. eikokuma

    eikokuma

    Joined:
    Mar 13, 2015
    Posts:
    4
    Like I said, I DID extend NetworkManager.
     
  6. chenyuchih

    chenyuchih

    Joined:
    Jun 3, 2015
    Posts:
    37
    Maybe you forgot "override" modifier?
    I make a test with unity 5.2.2p4. If I don't add override modifier, OnServerSceneChanged won't be invoked.
     
  7. Zullar

    Zullar

    Joined:
    May 21, 2013
    Posts:
    650
    I also have the same issue with OnServerSceneChanged not being called. Here is my code.

    Code (csharp):
    1.  
    2. public class BatNetworkManager : NetworkManager
    3. {
    4.     private void Awake()
    5.     {
    6.         //THIS CAUSES THE BUG!!!  Do not call Awake()!!!!
    7.     }
    8.     public override void OnServerSceneChanged(string sceneName)
    9.     {
    10.      
    11.         Debug.Log("OnServerSceneChanged called");
    12.     }
    13. }
    14.  
     
    Last edited: Nov 24, 2015
    maminaliari likes this.
  8. eikokuma

    eikokuma

    Joined:
    Mar 13, 2015
    Posts:
    4
    I do have the override modifier, so that's not the issue either. Sorry to hear you're having the same problem, Zullar :c
     
  9. chenyuchih

    chenyuchih

    Joined:
    Jun 3, 2015
    Posts:
    37
    Hi Eihiko and Zullar. Can you upload your project as a file?
     
  10. Zullar

    Zullar

    Joined:
    May 21, 2013
    Posts:
    650
    I *just* figured out what the issue was. I have a custom child NetworkManager where I called Awake(). Calling Awake() seems to override Unity's default NetworkManager initialization and causes various bugs such as:
    -NetworkManager.singleton not being set
    -SceneObjects not spawning (NetworkServer.SpawnObjects() isn't automatically called)
    -NetworkServer.OnServerSceneChanged() does not get called

    Hope this helps. It took me forever to track down this bug!

    I did a bug report and post here
    http://forum.unity3d.com/threads/unet-scene-objects-not-spawning-w-custom-networkmanager.369236/
     
    zeiksz likes this.
  11. sssachinvicky

    sssachinvicky

    Joined:
    Sep 16, 2019
    Posts:
    2
    HI. I know this post is really old. I am facing the same error in Mirror which is a community run equivalent for Unet. Stuck in this for weeks. I don't have an awake method in my custom networkManager. Someone Help.