Skip to content

Detect instantiated copies of scene objects from the scene object reg… - #1096

Open
jimdroberts wants to merge 1 commit into
FirstGearGames:mainfrom
jimdroberts:fix/sceneid-instantiated-copies
Open

jimdroberts wants to merge 1 commit into
FirstGearGames:mainfrom
jimdroberts:fix/sceneid-instantiated-copies

Conversation

@jimdroberts

Copy link
Copy Markdown
Contributor

Problem

4.7.3, scene NetworkObjects in a scene that is not in Build Settings are never spawned. This hits every project that loads scenes through Addressables or AssetBundles, on server and clients. Found in a production MMO project whose world scenes all load through Addressables.

The same change was meant to reset the SceneId on instantiated copies of scene objects (#1034), but in a Build Settings scene it does not detect copies.

Cause

4.7.3R (dad140e) moved the reset for copies from ServerObjects.Spawn into NetworkObject.Awake (Runtime/Object/NetworkObject/NetworkObject.cs L396 on main):

if (gameObject.scene.buildIndex == -1)
    SceneId = 0;

The premise, from #1034, was that an instantiated object has no scene yet during Awake. It does have one:

  • A scene object in a scene that is not in Build Settings runs Awake with buildIndex == -1, so its SceneId is cleared. ServerObjects.SceneManager_sceneLoaded then collects scene objects with ignoreUnsetSceneIds: true (L372), so the object is skipped and never set up or spawned. An object that is inactive at load is registered first and loses its SceneId later, when it is activated.
  • An instantiated copy runs Awake already in the active scene, with that scene's build index. In a Build Settings scene the check never fires for copies.

Fix

  • Remove the check from NetworkObject.Awake.
  • In ServerObjects.Spawn, reset the SceneId when a different object is registered under it in SceneObjects_Internal. That object is the original scene object the copy was made from: SetupSceneObjects registers every scene object, active or inactive, under its own SceneId (L479). A scene object passed to Spawn, including one that was inactive until now (the Scene object expecting prefab ID #1034 case), is registered under its own SceneId and keeps it.

The condition only resets when the registry proves the object is a copy. If the original is not registered (it was destroyed, or its scene has not been set up yet), the copy keeps its SceneId, which is what main does for copies in Build Settings scenes today.

Evidence

A probe in a fresh Unity 6000.4.10f1 project logs gameObject.scene during Awake from an [ExecuteAlways] component, so this is edit mode. Scenes were opened additively and objects instantiated with Object.Instantiate:

A pre-placed, scene NOT in Build Settings     valid=True name='NonBuildScene' buildIndex=-1 isLoaded=False
B pre-placed, scene IN Build Settings         valid=True name='BuildScene'    buildIndex= 1 isLoaded=False
C Instantiate(scene obj), active NOT in build valid=True name='NonBuildScene' buildIndex=-1 isLoaded=True
D Instantiate(prefab),    active NOT in build valid=True name='NonBuildScene' buildIndex=-1 isLoaded=True
E Instantiate(scene obj), active IN build     valid=True name='BuildScene'    buildIndex= 1 isLoaded=True
F Instantiate(prefab),    active IN build     valid=True name='BuildScene'    buildIndex= 1 isLoaded=True
H inactive copy, then SetActive (IN build)    valid=True name='BuildScene'    buildIndex= 1 isLoaded=True

A is the scene object that main wrongly clears. E and H are the copies that main misses. No scene property separates a pre-placed object that is activated later from a copy (H), which is why the fix uses the registry instead. The probe scripts are available on request.

The Spawn change is verified by reading the registration path above; I have not run it in play mode. Compiled against Unity 6000.4 with no new errors or warnings.

Behavior change

  • Scene objects in scenes outside Build Settings get their SceneId and spawn again, as before 4.7.3.
  • Copies of scene objects get their SceneId reset in Spawn whenever their original is registered, in any scene. Previously this only happened when the active scene was outside Build Settings.

…istry

4.7.3 moved the SceneId reset for instantiated copies of scene objects from
Spawn into NetworkObject.Awake, keyed on gameObject.scene.buildIndex == -1.
Objects placed in a scene that is not in Build Settings (Addressables or
AssetBundle scenes) also have buildIndex -1 in Awake, so every scene
NetworkObject in such a scene lost its SceneId and was never spawned. An
instantiated copy, meanwhile, already belongs to the active scene in Awake, so
the check did not detect copies in Build Settings scenes either.

Remove the Awake check. In Spawn, reset the SceneId only when a different
object is registered under it in SceneObjects, which is the original scene
object a copy was made from. Scene objects, including ones that stayed
inactive until spawned, are registered under their own SceneId and keep it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant