fix: guard TransformHandle against reflection serialization (destroyed-object NRE)#1274
Open
agudmund wants to merge 1 commit into
Open
fix: guard TransformHandle against reflection serialization (destroyed-object NRE)#1274agudmund wants to merge 1 commit into
agudmund wants to merge 1 commit into
Conversation
GameObjectSerializer reflects over component properties and hands values to Json.NET. On Unity 6, a property returning UnityEngine.TransformHandle makes the serializer enumerate the handle's direct children - which throws NullReferenceException (AssertHandleIsValid) the moment the underlying object is destroyed, a common race when component data is read during play mode while scripts Destroy() objects. On live objects it would walk entire child hierarchies of handles instead of a value. Add a write-only TransformHandleConverter that emits null for the type, matched by full name so the package still compiles on Unity versions without the type (floor is 2021.3), avoiding version defines per the Unity*Compat guidance. Internal for the same converter-scanner reason as UnityEngineObjectConverter (CoplayDev#1138). Repro: read mcpforunity://scene/gameobject/{id}/components repeatedly in play mode while a spawner destroys and respawns tagged objects. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Contributor
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (4)
📝 WalkthroughWalkthroughAdds a write-only Newtonsoft converter for Unity 6 ChangesTransformHandle serialization
Estimated code review effort: 2 (Simple) | ~10 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
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.
Why
GameObjectSerializerreflects over component properties and hands the values to Json.NET. On Unity 6, a component property returningUnityEngine.TransformHandlemakes the serializer enumerate the handle's direct children (TransformHandle.DirectChildrenEnumerator). Two problems fall out of that:NullReferenceExceptionfromTransformHandle.AssertHandleIsValidthe moment the underlying object is destroyed. Reading thegameobject/{id}/componentsresource while a game scriptDestroy()s objects (a spawner destroying and respawning pickups makes it reliable) logs the[GameObjectSerializer] Unexpected error serializing value of type UnityEngine.TransformHandlewarning on every read that races a destroy.What
A write-only
TransformHandleConverterthat emitsnullfor the type. It matches by full type name (UnityEngine.TransformHandle) so the package still compiles on every supported Unity version (floor 2021.3, where the type does not exist) without version defines, per theUnity*Compatguidance in CONTRIBUTING. It isinternalfor the same third-party converter-scanner reason asUnityEngineObjectConverter(#1138), and registered inGameObjectSerializer's output settings.EditMode tests resolve the type by reflection and self-ignore on Unity versions without it; the key regression test obtains a handle, destroys its object, and asserts serialization no longer throws.
Observed in the field on Unity 6000.4.10f1 while polling component resources during play mode.
🤖 Generated with Claude Code
Summary by CodeRabbit
Bug Fixes
TransformHandlevalues.nullin serialized output.Tests
TransformHandlescenarios.