Repository navigation
Infinite hang when reloading a scene contains a manually spawned NetworkObject with a SpriteLibrary and a SpriteResolver #4095
Copy link
Copy link
Closed
Labels
type:supportQuestions or other supportQuestions or other support
Description
Description
Infinite hang when reloading a scene contains a manually spawned NetworkObject with a SpriteLibrary and a SpriteResolver
Reproduce Steps
- Create an empty scene
- Add a NetworkManager, with UnityTransport
- Create a new prefab with NetworkObject, SpriteRenderer, Animator, SpriteLibrary, SpriteResolver (following basically this tutorial: https://www.youtube.com/watch?v=4xPHqEEa-V0)
- Create an empty object
TestCrashwith the following code, and link the prefab from (3) in the inspector
public class TestCrash : NetworkBehaviour
{
[SerializeField] public GameObject prefab = null!;
private bool _alreadyCalled;
private void Start()
{
var networkManager = NetworkManager.Singleton;
var unityTransport = networkManager.GetComponent<UnityTransport>();
// Start local server
unityTransport.UseWebSockets = false; // if applicable
unityTransport.SetConnectionData("0.0.0.0", 7777);
networkManager!.NetworkConfig.NetworkTransport = unityTransport;
networkManager.StartHost();
NetworkManager.Singleton.SpawnManager.InstantiateAndSpawn(
prefab!.GetComponent<NetworkObject>(),
OwnerClientId,
destroyWithScene: true,
position: Vector3.zero
);
}
private void Update()
{
if (_alreadyCalled) return;
if (Time.timeSinceLevelLoad < 3f) return;
_alreadyCalled = true;
NetworkManager.Singleton.SceneManager.LoadScene(SceneManager.GetActiveScene().name, LoadSceneMode.Single);
}
}
TestCrash is some code that
- starts a local server
- spawns the prefab
- waits 3 seconds
- reloads the scene
Actual Outcome
After a few attempts, the scene never reloads and the Unity editor hangs forever (has to be force quitted)
Expected Outcome
The scene reloads every 3s
Environment
- OS: Windows 11
- Unity Version: 6000.4.7f1
- Netcode Version: 2.12.0
Additional Context
- This bug did not happen with 2.12.0 and Unity 6000.3.11f1 (started happening after upgrading to 6000.4.7f1)
- The bug doesn't happen without
destroyWithScene: true, - Removing the SpriteResolver/SpriteLibrary stops the bug from happening
- The NetworkObject must be spawned with
InstantiateAndSpawn, if the prefab is added to the scene, the bug doesn't happen
Here is a dump of the Main Thread stack when the editor is hanging. The CPU is at 0% at that point. Maybe a lock contention?
<unknown> 0x00007ff973d63cb4
<unknown> 0x00007ff973c30543
<unknown> 0x00007ff97107e558
UnityClassic::Baselib_SystemFutex_Wait(int * __ptr64,int,unsigned int) 0x00007ff8886af7f6
ProfilerMutexLock(Mutex & __ptr64,char const * __ptr64,profiling::Marker & __ptr64,char const * __ptr64,int) 0x00007ff88b1a4450
PersistentManager::Lock(PersistentManager::LockFlags,profiling::Marker * __ptr64) __ptr64 0x00007ff88b1a1b83
PersistentManager::GetPathName(EntityId) __ptr64 0x00007ff88b1968bb
GetGUIDFromEntityId(EntityId) 0x00007ff888e786fe
AssetChangedNotifier::UnsubscribeFromAll(EntityId) __ptr64 0x00007ff88a209aee
SpriteRenderer::MainThreadCleanup(void) __ptr64 0x00007ff88b631e42
delete_object_internal_step1(Object * __ptr64) 0x00007ff88b8cd380
CommitBatchDelete(BatchDelete & __ptr64) 0x00007ff88b5286d2
DestroyObjectHighLevel_Internal(Object * __ptr64,bool) 0x00007ff88b532e31
DestroyObjectHighLevel(Object * __ptr64,bool) 0x00007ff88b532ba8
DelayedDestroyCallback(Object * __ptr64,void * __ptr64) 0x00007ff88b7963cb
DelayedCallManager::Update(int) __ptr64 0x00007ff88b797ac8
<unknown> 0x00007ff88b4f9d6b
ExecutePlayerLoop(NativePlayerLoopSystem * __ptr64) 0x00007ff88b517f5b
ExecutePlayerLoop(NativePlayerLoopSystem * __ptr64) 0x00007ff88b51811a
PlayerLoop(void) 0x00007ff88b51d105
EditorPlayerLoop::Execute(void) __ptr64 0x00007ff889ab957a
PlayerLoopController::InternalUpdateScene(bool,bool) __ptr64 0x00007ff889ab2a9c
PlayerLoopController::UpdateSceneIfNeededFromMainLoop(void) __ptr64 0x00007ff889ab4dad
Application::TickTimer(void) __ptr64 0x00007ff889acc4c3
<unknown> 0x00007ff8894bfb2c
UnityMain 0x00007ff8894c8693
<unknown> 0x00007ff6ae972f2a
<unknown> 0x00007ff97360e957
<unknown> 0x00007ff973caad6c
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
type:supportQuestions or other supportQuestions or other support
Activity
acavailhez commentedon Jul 23, 2026
Actually it seems like SpriteLibrary/SpriteResolver are not needed for the bug to happen, simply replacing the sprite triggers the bug:
NoelStephensUnity commentedon Jul 23, 2026
Hi @acavailhez,
Recent post above:
Is this a typo?
go.GetComponent<SpriteRenderer>.sprite = [other sprite]It should be this:
go.GetComponent<SpriteRenderer>().sprite = [other sprite]Issue posted:
Issue-1: Destroy with scene on a spawned instance just prior to performing a LoadSceneMode.Single scene loading event
It looks like you are spawning a prefab instance in the currently active scene (where everything will be instantiated) and then immediately doing a scene loading event using
LoadSceneMode.Singlewhich will destroy anything in the currently active scene upon finishing the scene loading event (i.e. your spawned object is getting destroyed).Issue-2: For integrated scene management, use the
NetworkSceneManagercompleted events orOnSceneEventThe supported way to know a netcode synchronized scene event is finished is to use the
NetworkSceneManagerevents and not theUnityEngine.SceneManagement.SceneManagerdrivenTime.timeSinceLevelLoadas that can lead to scenarios whereUnityEngine.SceneManagement.SceneManageris finished butNetworkSceneManageris not.Issue-3: Using a
NetworkBehaviourto start theNetworkManagerWe do not recommend using a
NetworkBehaviourto start theNetworkManagerand while it might work under certain conditions, it coudl also lead to unexpected results/issues. The recommend component to use for startingNetworkManageris to use aMonoBehaviourand I personally would recommend using the pattern of placing that on the sameGameObjectas theNetworkManager.Resolution fix
Here is a quick modification of your script which should work.
Let me know if this resolves your issues?
acavailhez commentedon Jul 23, 2026
Hello @NoelStephensUnity!
About Issue 1
TestCrashwaits 3 seconds to reload the scene (it's not immediate)About Issue 2
I don't think the issue is related to scene lifecycle, I changed
TestCrashto wait 1 second before spawning the object and the bug still occursAbout Issue 3
When
TestCrashis aMonoBehaviorthe issue still occursHere's a simpler
TestCrashto reproduce the issue:The first reload works, but the second reload hangs forever
NoelStephensUnity commentedon Jul 23, 2026
Here is an exported package that you can import into your project and it does exactly what you are trying to do but does not lock or crash the editor.
Issue4095.zip
Once imported, add the TestScene to your scenes in build list, open it, and enter play mode.
The provided contents of the exported package continually reloads the scene every 3 second (what I had set as the default), changes the sprite on a newly spawned object, and will not cause any race conditions/crashes to occur.
This line:
NetworkManager.Singleton.SceneManager.LoadScene(SceneManager.GetActiveScene().name, LoadSceneMode.Single);Coupled with how you are checking if the scene is loaded is where you are running into issues.
This code (based on my first response):
if (Time.timeSinceLevelLoad < 1f) return;Is not a Netcode for GameObjects supported way to determine if a scene loading event has completed or not. This is actually a portion of the reason why you are having issues with your approach. I am just trying to point out that you are using a technique that we don't support so if you run into issues using this approach there is not much we can do to help you resolve that issue.
The other issue is that you are reloading the same scene that has your NetworkManager within it...thus you are creating a new instance that is automatically migrated into the DDOL each time you reload the same scene.
Here is a modified version of your project that has all sorts of catches in it where you could run into issues.
The first issue it will run into is that you are creating more than one NetworkManager instance.
It stops after the 1st scene load because it destroys the 2nd NetworkManager.
If you were to adjust your code to assure that the 1st instance (pre-loaded scene) will create a Coroutine to reload the scene after (n) seconds (as is done in the script provided in the above exported package) then it will start working more as expected...but you still need to destroy that additional NetworkManager.
You want to use m_NetworkManager.SceneManager.OnLoadEventCompleted to signal that everyone has loaded and processed everything within the newly loaded scene. We support that approach and if you run into issues using that approach we can help you resolve your issue or if it is a bug we can fix it.
If you want to use this pattern (i.e. just reload the same currently loaded active scene over and over), then you would most likely want to use a "BootStrap" scene approach where: (I did not make these adjustments to your script)
(The example in the package I provided does not do this but does show that you can accomplish what you are trying to do if you use the supported way to handle detecting when a scene loading event has completed.)
Here are some links to our documentation that you might find useful
Don't use NetworkBehaviour to start NetworkManager
Notifications for instantiating and destroying NetworkManager
When is a scene truly loaded
Using OnSceneEvent example
Timing Considerations
Let me know if this helps you resolve your issue?
8 remaining items
EmandM commentedon Aug 11, 2026
Ah, I think I understand. Just to clarify, your core issue is that the Unity Editor is hanging, not that your Netcode logic isn't working? You're right that it's not expected that the Unity editor hangs forever in this case. We've not been able to reproduce the engine hang on our end.
This github repo is for specifically Netcode for GameObjects issues. Seeing as this editor hang crosses a few product boundaries it's going to be easiest for us to address if you can report a bug in the editor. Feel free to link your case number here and we can follow up on your report to ensure it gets to the right place.
NoelStephensUnity commentedon Aug 12, 2026
@acavailhez
I did replicate the issue and tried several combinations to resolve the issue. (Using 6000.4.0f1)
It does appear (oddly enough) that only if you replace the sprite and mark the instance to not destroy with the scene load (single mode) then (on my side using Windows 11) it ends up freezing the editor (attaching with a debugger isn't very useful either under this scenario as well).
However, if I comment out the code that swaps the sprite, then it runs perfectly fine. (🔥)
As @EmandM mentioned, this does seem like an editor specific issue that you can file as an editor related bug report via the editor.
However, we understand the pain associated with not being able to achieve something that does seem simple but ends up being an edge case issue. When using
LoadSceneMode.Single, it places a lot of the control over to the SceneManager for things like when a scene is fully unloaded (and the assets within the scene) which could potentially expose a bug under very specific timing related circumstances.Typically, I recommend using an additive scene loading flow. This provides you with a bit more control over when you want to unload or load a scene and you can change the timing for both based on your project's needs.
As such, here is an example project that accomplishes the same thing you were trying to do using
LoadSeneMode.Singlebut it uses the additive scene loading pattern:GH-4095-Solution.zip
Below is a video of the project running and shows you can switch the mode (destroy with scene or don't destroy with scene) on the fly in playmode and it continues to work whether destroying or not destroying when the scene it is currently within is unloaded (and it replaces the sprite on the newly instantiated and spawned instance):
GH-4095-Solution.mov
Let me know if this approach works for you (at least until the current issue when using SingleMode scene loading is resolved)?
acavailhez commentedon Aug 18, 2026
Hi @NoelStephensUnity !
Thanks a lot for your time and effort!
I think additive scene loading is not going to work for what I'm doing, but I appreciate the idea.
The lock contention only happens in the editor so I have a workaround for now. I'd to remove it one day!
NoelStephensUnity commentedon Sep 25, 2026
@acavailhez
This has been identified as an engine issue and should be resolved (currently) in the following editor versions:
6000.3.24f1, 6000.5.3f1 and 6000.6.
No 6000.4 released build has this fix in it (you reported you were using 6000.4.7f1, which means the bug still exists on this version of Unity).
My recommendation would be to update to 6000.5.3f1 or higher to resolve the underlying issue.
Let me know if this (finally) resolves your issue?
michalChrobot commentedon Sep 25, 2026
To add to that 6000.4 and 6000.5 are out of support already and won't receive further updates so preferably you would move to 6000.6 (although latest 6000.5 should also resolve your issue, just remember that the editor itself won't get further updates)
With that I will close the issue but we can always reopen if this won't fix the issue