After entering a scene and spawning NetworkObjects, I shut down the NetworkManager. However, when transitioning to another scene and starting as a host again, the game repeatedly logs the following error:
MissingReferenceException: The object of type 'Unity.Netcode.NetworkObject' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
UnityEngine.Object+MarshalledUnityObject.TryThrowEditorNullExceptionObject (UnityEngine.Object unityObj, System.String parameterName) (at /Users/bokken/build/output/unity/unity/Runtime/Export/Scripting/UnityEngineObject.bindings.cs:869)
UnityEngine.Bindings.ThrowHelper.ThrowNullReferenceException (System.Object obj) (at /Users/bokken/build/output/unity/unity/Runtime/Export/Scripting/BindingsHelpers.cs:61)
UnityEngine.Component.get_gameObject () (at <eac12af5e0034b02b1bfe348a7feb8c6>:0)
Unity.Netcode.NetworkObject.UpdateForSceneChanges () (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs:3318)
Unity.Netcode.NetworkObject.UpdateNetworkObjectSceneChanges () (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs:3287)
Unity.Netcode.NetworkManager.NetworkUpdate (Unity.Netcode.NetworkUpdateStage updateStage) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs:404)
Unity.Netcode.NetworkUpdateLoop.RunNetworkUpdateStage (Unity.Netcode.NetworkUpdateStage updateStage) (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Core/NetworkUpdateLoop.cs:191)
Unity.Netcode.NetworkUpdateLoop+NetworkPostLateUpdate+<>c.<CreateLoopSystem>b__0_0 () (at ./Library/PackageCache/com.unity.netcode.gameobjects/Runtime/Core/NetworkUpdateLoop.cs:286)
The scene transition sequence is as follows:
-
Start the project in Scene A.
-
In Scene B, start the host and spawn NetworkObjects.
-
Shut down the NetworkManager and return to Scene A
-
Move to Scene B, start as a host again, and encounter continuous error logs.
Edit: Mistype
Edit: After doing a rollback to the version 1.12.2 everything worked fine but I really need the new updates and implementations of the 2+ versions.
Can you post your code and details about your setup?
The repeated cycle of starting and shutting down is where things often go wrong because a) shutdown is not instantaneous for the server/host and b) there may be lingering references, most commonly returning to a scene with the NetworkManager on it, thus duplicating it (and then even adding code to destroy one of them to “fix the duplication” which only leads to follow-up issues).
The code is very basic:
- The network spawn of objects happens on those lines:
GameObject gameObject = Instantiate(prefab, randomPos, randomRotation);
NetworkObject networkObject = gameObject.GetComponent<NetworkObject>();
networkObject.Spawn();
- The Shutdown happens as this:
private void Awake()
{
NetworkManager.Singleton.OnClientStopped += OnClientStopped;
}
private void OnClientStopped(bool value)
{
SceneManager.LoadScene(0);
}
public void ExitButtonAction(
{
NetworkManager.Singleton.Shutdown();
}
This would load the very first scene in the build list, which means NetworkManager is likely in that scene and thus gets duplicated (loaded again). Check the DontDestroyOnLoad subscene after exiting a game session.
If you’ll find two NetworkManagers:
Add an empty scene as build index 0 that has nothing but NetworkManager in it, plus any other global “don’t destroy on load” objects. From that scene 0, launch scene #1 with a simple script (call LoadScene in Update, not Awake or OnEnable because that isn’t allowed).
When exiting the session, load scene #1 too. This will properly prevent the NetworkManager from being duplicated when exiting sessions.
I considered that, but unfortunately, there was only one NetworkManager in the hierarchy. I also tried removing it from the scenes to prevent duplication, as you suggested, but the result was the same
I am also running into this error, I am using Unity 6000.0.32f, which is LTS so it should not have such an important error.
By what you said you solved the error only by switching to another version of Unity, 6000.1.12.2 I guess, Is that it? Did you find any other clues on what could cause this error?
Thanks!
Netcode is a package which is different from the editor version. Any package can have serious bugs, regardless of the editor version it’s being used in.
Also 6000.0.0f1 was also an LTS version and it definitely had a lot more issues than more recent versions. LTS versions aren’t by definition free of (serious) bugs. LTS literally means “long term support” so you’re guaranteed to get bugfix (aka “patch”) releases for at least two years.
Hello! The error is a known issue on the latest 2.* versions of Netcode. I’ve opened an issue and they are still working on that. Here’s the link: NetworkManager shutdown maintains references for the next scene · Issue #3218 · Unity-Technologies/com.unity.netcode.gameobjects · GitHub
The only workaround for now is to downgrade your Netcode version any of the 1.* versions.
Thanks, just was wondering if Unity version could have something to do with this error, and sure LTS have errors
just waiting for the patch.
Okey many thanks! I will be following closely this topic.
1 Like