Search Unity

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

Bug OnConfigurationChanged gets called before the NetworkSpawnManager gets created.

Discussion in 'Multiplayer' started by JakeManfreDev, Feb 11, 2024.

  1. JakeManfreDev

    JakeManfreDev

    Joined:
    Jul 9, 2018
    Posts:
    1
    I've been getting a
    NullReferenceException
    when starting my scene that has a
    NetworkManager
    in it that the
    SpawnedObjects
    is
    null
    when
    GetGameObject
    is called in
    Ngo1Adapter
    . The reason it is
    null
    is because the
    Spawnedobjects
    come from the
    NetworkSpawnManager
    which is created by the
    NetworkManager
    in the
    Initialize
    method. That
    Initialize
    method doesn't get called until a client, server, or host is started. So when the scene first starts playing, before a client, server, or host has started, an error is thrown because the
    VisualizationSystem
    calls
    OnConfigurationChagned
    in its constructor which was created in
    OnStart
    in
    NetVisContext
    that is called from the Unity lifecycle method
    Start
    from the
    RuntimeUpdater
    . You can see this in the
    Setup
    method in
    NetVisContext
    .

    Why is there a forced configuration change call before the
    NetworkManager
    has created the
    NetworkSpawnManager
    ? This error is very annoying.

    Not sure the implications of this but it seems like
    OnConfigurationChanged(configurationWithEvents.Configuration);
    should be removed on line 41 in
    VisualizationSystem
    . If I comment out that line I no longer get the error. I would think when you subscribe to the
    ConfigurationChanged
    event on line 36 above it, it should handle any configuration changes, unless you are concerned the network was started before the visualization system is constructed and you assume there was a config change?

    Here is the stack trace I get
    Code (CSharp):
    1.  
    2. NullReferenceException: Object reference not set to an instance of an object
    3. Unity.Multiplayer.Tools.Adapters.Ngo1.Ngo1Adapter.GetGameObject (Unity.Multiplayer.Tools.Adapters.ObjectId objectId) (at ./Library/PackageCache/com.unity.multiplayer.tools/Adapters/Ngo1/Ngo1Adapter.cs:159)
    4. Unity.Multiplayer.Tools.NetVis.Editor.Visualization.NetVisDataStore.GetGameObject (Unity.Multiplayer.Tools.Adapters.ObjectId objectId) (at ./Library/PackageCache/com.unity.multiplayer.tools/NetVis/Editor/Visualization/Systems/NetVisDataStore.cs:208)
    5. Unity.Multiplayer.Tools.NetVis.Editor.Visualization.MeshShading.ComputeNetworkedObjectColors (System.Collections.Generic.IReadOnlyList`1[T] objectIds) (at ./Library/PackageCache/com.unity.multiplayer.tools/NetVis/Editor/Visualization/MeshShading/MeshShading.cs:93)
    6. Unity.Multiplayer.Tools.NetVis.Editor.Visualization.MeshShading.UpdateObjectColors () (at ./Library/PackageCache/com.unity.multiplayer.tools/NetVis/Editor/Visualization/MeshShading/MeshShading.cs:76)
    7. Unity.Multiplayer.Tools.NetVis.Editor.Visualization.MeshShading.OnConfigurationChanged (Unity.Multiplayer.Tools.NetVis.Configuration.NetVisConfiguration configuration) (at ./Library/PackageCache/com.unity.multiplayer.tools/NetVis/Editor/Visualization/MeshShading/MeshShading.cs:55)
    8. Unity.Multiplayer.Tools.NetVis.Editor.Visualization.MeshShading..ctor (Unity.Multiplayer.Tools.NetVis.Configuration.NetVisConfiguration configuration, Unity.Multiplayer.Tools.NetVis.Editor.Visualization.NetVisDataStore netVisDataStore) (at ./Library/PackageCache/com.unity.multiplayer.tools/NetVis/Editor/Visualization/MeshShading/MeshShading.cs:41)
    9. Unity.Multiplayer.Tools.NetVis.Editor.Visualization.VisualizationSystem.OnConfigurationChanged (Unity.Multiplayer.Tools.NetVis.Configuration.NetVisConfiguration configuration) (at ./Library/PackageCache/com.unity.multiplayer.tools/NetVis/Editor/Visualization/Systems/VisualizationSystem.cs:87)
    10. Unity.Multiplayer.Tools.NetVis.Editor.Visualization.VisualizationSystem..ctor (Unity.Multiplayer.Tools.NetVis.Configuration.NetVisConfigurationWithEvents configurationWithEvents, Unity.Multiplayer.Tools.NetVis.Editor.Visualization.NetVisDataStore netVisDataStore, Unity.Multiplayer.Tools.Common.IRuntimeUpdater runtimeUpdater) (at ./Library/PackageCache/com.unity.multiplayer.tools/NetVis/Editor/Visualization/Systems/VisualizationSystem.cs:41)
    11. Unity.Multiplayer.Tools.NetVis.Editor.Visualization.NetVisContext.OnStart () (at ./Library/PackageCache/com.unity.multiplayer.tools/NetVis/Editor/Visualization/NetVisContext.cs:58)
    12. Unity.Multiplayer.Tools.Common.RuntimeUpdaterBehaviour.Start () (at ./Library/PackageCache/com.unity.multiplayer.tools/Common/Runtime/Helpers/RuntimeUpdaterBehaviour.cs:240)
    13.  
     
    Last edited: Feb 11, 2024
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,128
    I can only repeat what I recommend everywhere: put your NetworkManager in the very, very first scene of the game. In fact, it should be a "boot" scene that immediately loads the next scene in the sequence, for example the menu screen or when using corresponding command line options it goes straight to an ingame scene.

    This solves so many issues because it makes the NetworkManager singleton available throughout the entire lifecycle of the application, and not dependent on the order of execution when loading a scene and it avoids duplicating the NetworkManager when reloading the scene with the NetworkManager in it since the "boot" scene is not re-entrant (never loaded again).

    Sounds like this might also solve your issue, if only through refactoring the game architecture.