Unspawn + AdditiveSceneLoad force stops play mode when in Unity Editor.
**Describe the bug**
Let's say you play your application in the editor. If it calls `NetworkServer.UnSpawn(instance);` and then some time later loads an additive scene, Mirror will force stop the playback of the application.
**[IMPORTANT] How can we reproduce the issue, step by step:**
Use the **MirrorTopDownShooter** example:
1. Create a **new scene** without a camera and name it _'test'_. In this scene I have a Canvas with an EventSystem, but I think any empty GameObject in there will do as well (maybe even no game object, we just need Unity to load this scene additively)
2. Add the new **test** scene to the build list. File -> Build Profiles -> Scene List -> Add Open Scenes.
3. Open **MirrorTopDownShooter** scene again.
4. In MirrorTopDownShooter create an empty GameObject and add a new script to it. I called mine "Reproducer" with the following code:
```
using Mirror;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Reproducer : MonoBehaviour
{
[SerializeField] GameObject spawnThis;
[SerializeField] string sceneName = "test";
[SerializeField] private GameObject instance;
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Alpha1))
{
Setup();
}
if (Input.GetKeyDown(KeyCode.Alpha2))
{
Reproduce();
}
}
void Setup()
{
instance = Instantiate(spawnThis, Vector3.zero, Quaternion.identity);
NetworkServer.Spawn(instance);
}
void Reproduce()
{
NetworkServer.UnSpawn(instance);
SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);
}
}
```
5. In the Editor, on the Reproducer object, link the **SpawnThis** variable to **EnemyPrefab** from **MirrorTopDownShooter**'s prefabs.
6. On the same object, set **SceneName** to _'test'_. Ok we're ready to see it happen.
7. Start the application (press Play in the editor, we're still in **MirrorTopDownShooter** scene)
8. Once the application starts, click "Player Host".
9. Now on the keyboard press 1. This will spawn an enemy (confirm that by looking at the object hierarchy)
10. Now on the keyboard press 2. This will despawn the enemy and load the additive scene. Very shortly after the application will stop running suddenly. (confirm by verifying that the application is no longer in play mode).
An exception will be thrown and logged:
`Scene Assets/Mirror/Examples/TopDownShooter/Scenes/MirrorTopDownShooter.unity needs to be opened and resaved, because the scene object EnemyPrefab(Clone) has no valid sceneId yet.
UnityEngine.Debug:LogError (object)
Mirror.NetworkScenePostProcess:OnPostProcessScene () (at Assets/Mirror/Editor/NetworkScenePostProcess.cs:77)
UnityEditor.Build.BuildPipelineInterfaces:OnSceneProcess (UnityEngine.SceneManagement.Scene,UnityEditor.Build.Reporting.BuildReport)`
## Root cause:
Mirror's **NetworkScenePostProcess** kicks in every time a scene is loaded. It goes through all of the NetworkIdentity objects. If any object has `sceneId == 0 && !identity.isClient && !identity.isServer` Mirror will log the above error and stop the application.
As far as I can tell this is the mechanism for detecting misconfigured scene objects and notifying the user. However, this also happens if you call `NetworkServer.UnSpawn(instance);` and then load a scene additively. This is because UnSpawn calls ResetState which sets **isServer** and **isClient** both to **false**, causing the scene post processing to missinterpret the unspawned object as misconfigured.
**Expected behavior**
Mirror should not stop the application and not log the above error when a spawned prefab gets unspawned followed by an additive scene load.
**Desktop (please complete the following information):**
* OS: \[Windows\]
* Build target: \[Not relevant, but Windows\]
* Unity version: \[6000.0.59f2]
* Mirror branch: \[96.0.1\]
关闭于 2025-12-25 5 条评论