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. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    Dismiss Notice

Error loading new scene

Discussion in 'Multiplayer' started by dkalkwarf, Feb 28, 2023.

  1. dkalkwarf

    dkalkwarf

    Joined:
    Mar 29, 2020
    Posts:
    13
    I am creating a two-player game. I am having trouble with the transition from a menu scene to the game scene. In general, my problem is how to wait for all clients to connect, switch scenes, and know when all clients have been loaded.

    I am using NetworkManager.OnClientConnectedCallback to determine when all clients are connected. This works fine. After all clients are connected I call NetworkManager.ScreenManager.LoadScene to load my game scene (and unload my menu scene). Prior to calling LoadScene I subscribe to SceneManager.OnSceneEvent to determine when all clients have been loaded. Note that I load and subscribe within the OnClientConnectedCallback handler - not sure if this makes a difference.

    Here is where my problems and confusion begin. I am unsure how to call LoadScene and subscribe to OnSceneEvent in one scene and determine that all clients have been loaded in another.

    I thought I would have to mark the object containing the OnSceneEvent handler as Dont Destroy With Owner. I thought that this would put the object in my new scene to wait for all clients to load. This didn't work. I get a runtime warning and error.

    The warning is for the object that handles the button pushes that call StartHost and StartClient. The error is "[object] is being destroyed while object is still spawned." This confuses me since it seems like that is what is supposed to happen: the spawned object should be destroyed when switching scenes. Also, this doesn't much sense to me since there are other objects in the scene (e.g. buttons) that Unity doesn't complain about.

    The error is for the object containing the OnSceneEvent handler that I marked Dont Destroy With Owner. The error is "The object of type 'SceneSwitcher' has been destroyed but you are still trying to access it."

    For starters, it seem that I do not understand Dont Destroy With Owner. My understanding is that the object will be put into the new scene with its state intact. One question: Do I have to have the same object in each scene? When marked as Dont Destroy With Owner does Unity simply replace that object in the new scene with the previous instance (and not instantiate a new one)?

    Any help is appreciated. If my approach is poor, please give me a hint on a better one. My scenario (menu scene, wait for players, switch scenes, wait for everyone to be loaded.) seems very common.
     
  2. ep1s0de

    ep1s0de

    Joined:
    Dec 24, 2015
    Posts:
    163
    Why disconnect clients when changing the scene when you can create an event message that will force them to load the scene and if there was a loading error on the client or something else, tell the server back that something like this happened...
     
  3. dkalkwarf

    dkalkwarf

    Joined:
    Mar 29, 2020
    Posts:
    13
    I am not sure what you mean by "disconnect." I am just calling LoadScene to load another scene (and unload the menu scene). Then I wait until the clients are loaded.

    At any rate, I have made some progress.

    I discovered the LoadSceneCompleted event. This simplified my task of knowing when all clients are loaded.

    Also, I managed to not destroy my SceneSwitcher object and move it to the new scene. It didn't seem to work before by checking the DontDestroyWithOwner property. I managed to make it work by calling the DontDestroyOnLoad method.

    However, I still have a problem. I have a GameManager object in my game scene, but not the menu scene. I wish to access this object in my SceneSwitcher object, but cannot seem to find the right call to GetComponentxxx. Is this because the SceneSwitcher object is in the hierarchy in DontDestroyOnLoad and the GameManager object is not? I suppose I could make the GameManager a singleton. Or I could put it in the menu scene (and not destroy it) even though it does nothing there.
     
  4. ep1s0de

    ep1s0de

    Joined:
    Dec 24, 2015
    Posts:
    163
    Why move something when components such as:
    NetworkManager
    SceneSwitcher

    They must be indestructible using DontDestroyOnLoad and always exist from the start of the game, the logic of the client and server (in general, all events: loading the scene, etc.) must be called using network messages