Skip to content

Fix Unity 6000.6 InstanceIDToObjectCompat NotImplementedException - #1399

Merged
Scriptwonder merged 1 commit into
CoplayDev:betafrom
yuseres:fix/6000-6-instanceid-resolve
Sep 30, 2026
Merged

Scriptwonder merged 1 commit into
CoplayDev:betafrom
yuseres:fix/6000-6-instanceid-resolve

Conversation

@yuseres

@yuseres yuseres commented Sep 13, 2026

Copy link
Copy Markdown

Summary

Fixes #1398.

On Unity 6000.6.0f1, UnityObjectIdCompat.InstanceIDToObjectCompat(int) throws NotImplementedException for every call. This breaks manage_components (set_property/add) whenever GameObjectLookup resolves a target — including search_method="by_name", since FindByTarget round-trips through an instance ID internally — and breaks the gameobject/{id} and gameobject/{id}/components resources entirely.

Root cause

Verified via reflection against a live 6000.6.0f1 Editor: both EditorUtility.InstanceIDToObject(int) and EntityId's implicit int→EntityId conversion are unimplemented stubs at runtime on 6000.6.0f1 — not merely obsolete-marked. The existing CS0619-avoidance reflection shim successfully resolves the MethodInfo, but invoking it always throws.

The only reverse-resolution path that actually works on 6000.6 is:

EditorUtility.EntityIdToObject(EntityId.FromULong(fullUlong))

...which needs the full 64-bit id. GetInstanceIDCompat (forward direction) already discards the high 32 bits by truncating to int for wire compatibility, so by the time InstanceIDToObjectCompat receives the int, there's no way to reconstruct a valid EntityId from it alone.

Fix

Cache the full 64-bit value behind the truncated int at mint time (in GetInstanceIDCompat), and use that cache to resolve in InstanceIDToObjectCompat instead of trying to rebuild the EntityId from the int. This:

  • Keeps the existing int wire format and the already-documented session-scoped id contract (a static Dictionary naturally resets on domain reload, matching that scope)
  • Requires no changes to the ~30 call sites across the package or to the Python-side protocol
  • Only touches UnityObjectIdCompat.cs

Testing

Verified end-to-end against a live Unity 6000.6.0f1 project:

  • manage_components set_property targeting a GameObject by name (previously threw TargetInvocationException → NotImplementedException) — now succeeds
  • manage_components set_property targeting by a cross-call instance ID (obtained from an earlier find_gameobjects call) — now succeeds and actually writes the value
  • mcpforunity://scene/gameobject/{id}/components resource read — now returns data instead of erroring
  • No new console errors after the fix; clean recompile

🤖 Generated with Claude Code

On Unity 6000.6.0f1, both EditorUtility.InstanceIDToObject(int) and
EntityId's implicit int->EntityId conversion are unimplemented stubs at
runtime (verified via reflection against a live 6000.6.0f1 Editor) even
though the CS0619-avoidance reflection shim resolves the MethodInfo
successfully. Every call into InstanceIDToObjectCompat therefore threw
NotImplementedException, breaking manage_components (set_property/add)
whenever GameObjectLookup resolved a target - including by_name lookups,
since FindByTarget round-trips through an instance ID internally - and
breaking the gameobject/{id} and gameobject/{id}/components resources.

The only reverse-resolution path that works on 6000.6 is
EditorUtility.EntityIdToObject(EntityId.FromULong(fullUlong)), which
needs the full 64-bit id. GetInstanceIDCompat already discards the high
32 bits by truncating to int for wire compatibility, so by the time
InstanceIDToObjectCompat receives the int there's no way to reconstruct
a valid EntityId from it alone.

Cache the full 64-bit value behind the truncated int at mint time and
use that cache to resolve instead of trying to rebuild the EntityId
from the int. This keeps the existing int wire format and the
already-documented session-scoped id contract, with no changes needed
to any of the ~30 call sites or the wire protocol.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@coderabbitai

coderabbitai Bot commented Sep 13, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

Note

Currently processing new changes in this PR. This may take a few minutes, please wait...

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: 3437afc9-adc8-42bc-b84a-3a156b847b9c

📥 Commits

Reviewing files that changed from the base of the PR and between 8be7d96 and e447a8e.

📒 Files selected for processing (1)
  • MCPForUnity/Runtime/Helpers/UnityObjectIdCompat.cs
 ________________________________________________________________________________________________________________________
< Don't worry if it doesn't work right. If everything did, you'd be out of a job. - Mosher's Law of Software Engineering >
 ------------------------------------------------------------------------------------------------------------------------
  \
   \   \
        \ /\
        ( )
      .( o ).
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Autopilot is currently an internal CodeRabbit preview.


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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Scriptwonder

Copy link
Copy Markdown
Collaborator

Hi, can you rebase it to beta instead of main?

@Enough1122

Copy link
Copy Markdown

AI code review — automated review for reference; please use your judgment.

This replaces the reflection-based InstanceIDToObject(int) lookup on Unity 6000.6+ — which the author found throws NotImplementedException at runtime — with a static Dictionary<int, ulong> populated at mint time in GetInstanceIDCompat. The diagnosis is well evidenced, and keeping the int wire format avoids touching ~30 call sites. The concern is what the cache trades away: it turns a previously-throwing path into a silently-wrong one, and nothing in CI compiles it.

  1. Truncation collisions now resolve to the wrong object instead of throwing. MCPForUnity/Runtime/Helpers/UnityObjectIdCompat.cs:50 stores full under (int)full, so two objects whose EntityIds differ only in the high 32 bits share a key and the later mint overwrites the earlier one. A client holding the id for object A then gets object B from MCPForUnity/Runtime/Helpers/UnityObjectIdCompat.cs:71 with no error at all. On the base branch 6.6 threw on every call, so wrong-object was not previously reachable. Please document the collision in the docstring at MCPForUnity/Runtime/Helpers/UnityObjectIdCompat.cs:63 or key the cache on something collision-free.

  2. The cache is unbounded and never invalidated. MCPForUnity/Runtime/Helpers/UnityObjectIdCompat.cs:30 grows by one entry per distinct object touched, with no eviction on destroy, scene load or play-mode transition, while GetInstanceIDCompat sits on hot paths (GameObjectLookup.SearchByName, and GameObjectResource.SerializeGameObject for every child and parent). In a long editor session with scene churn that is a slow leak, and stale entries outlive the objects they describe. The repo deliberately runs PlayMode tests with domain reload disabled (TestRunnerService.EnsurePlayModeRunsWithoutDomainReload), which is exactly the window where statics survive but engine-side identity does not.

  3. No CI job compiles this branch. tools/unity-versions.json names this file in $coverageGap as unexercised, and compile-check.yml pins defaultVersion: 6000.0.75f1, where UNITY_6000_6_OR_NEWER is off — so the new code is never even type-checked, let alone run. Since the change exists specifically to fix 6000.6, please get a 6000.6+ project into the matrix or at least into the compile defines.

@Scriptwonder
Scriptwonder changed the base branch from main to beta September 30, 2026 18:11
@Scriptwonder
Scriptwonder merged commit f8d3088 into CoplayDev:beta Sep 30, 2026
6 of 7 checks passed
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.

Unity 6000.6: InstanceIDToObjectCompat throws NotImplementedException for every call, breaking manage_components and gameobject resources

4 participants