Repository navigation
Detect instantiated copies of scene objects from the scene object reg… - #1096
Open
jimdroberts wants to merge 1 commit into
Open
jimdroberts wants to merge 1 commit into
jimdroberts wants to merge 1 commit into
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.SpawnintoNetworkObject.Awake(Runtime/Object/NetworkObject/NetworkObject.csL396 on main):The premise, from #1034, was that an instantiated object has no scene yet during Awake. It does have one:
buildIndex == -1, so its SceneId is cleared.ServerObjects.SceneManager_sceneLoadedthen collects scene objects withignoreUnsetSceneIds: 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.Fix
NetworkObject.Awake.ServerObjects.Spawn, reset the SceneId when a different object is registered under it inSceneObjects_Internal. That object is the original scene object the copy was made from:SetupSceneObjectsregisters 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.sceneduring Awake from an[ExecuteAlways]component, so this is edit mode. Scenes were opened additively and objects instantiated withObject.Instantiate: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