Skip to content

fix: register undo when deleting GameObjects via manage_gameobject#1135

Open
Dallon wants to merge 1 commit into
CoplayDev:betafrom
Dallon:fix-mcp-delete-undo
Open

fix: register undo when deleting GameObjects via manage_gameobject#1135
Dallon wants to merge 1 commit into
CoplayDev:betafrom
Dallon:fix-mcp-delete-undo

Conversation

@Dallon
Copy link
Copy Markdown

@Dallon Dallon commented May 18, 2026

Summary

manage_gameobject(action="delete") calls Object.DestroyImmediate directly, bypassing Unity's undo stack — so manage_editor(action="undo") cannot recover GameObjects deleted via MCP. This one-line fix switches to Undo.DestroyObjectImmediate, restoring expected editor-undo behavior with full reference-graph restoration.

The package already uses Undo.DestroyObjectImmediate correctly elsewhere — ComponentOps.cs:130, GameObjectComponentHelpers.cs:88,137. GameObjectDelete.cs was the outlier.

Why the existing comment was stale

// Note: Undo.DestroyObjectImmediate doesn't work reliably in test context,
// so we use Object.DestroyImmediate. This means delete isn't undoable.
// TODO: Investigate Undo.DestroyObjectImmediate behavior in Unity 2022+

package.json targets unity: "2021.3". The TODO is from that era — Undo.DestroyObjectImmediate has been stable across all currently-supported Unity versions. The new undo test plus the existing 11-test baseline pass without regression on Unity 6000.3.10f1.

Tests

Added Delete_RegistersUndo_RestoresOnUndo to the existing ManageGameObjectDeleteTests class — creates a GameObject via new GameObject(name) (no prior undo registration), invokes the delete handler, asserts the object is gone, calls Undo.PerformUndo(), asserts restoration.

Verified

  • Existing 11 ManageGameObjectDeleteTests still pass
  • New undo test passes
  • Live MCP roundtrip on Unity 6000.3.10f1: manage_gameobject(action="delete")manage_editor(action="undo") restores the object with the same instanceID

Related, not included

ManagePrefabs.cs has several other raw Object.DestroyImmediate call sites (lines ~881, ~1112, ~1119, ~1136, ~1148, ~1198) with the same bug shape — they deserve a separate audit/PR. Kept out of this change to keep scope tight to the manage_gameobject delete path.

Co-authored-by: Claude Opus 4.7 (1M context) noreply@anthropic.com

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • GameObject deletion operations now properly integrate with Unity's Undo/Redo system, allowing users to undo delete actions.
  • Tests

    • Added test coverage verifying GameObject deletion operations register correctly with the Undo system.

Review Change Stack

manage_gameobject(action="delete") called Object.DestroyImmediate directly,
bypassing Unity's undo stack — manage_editor(action="undo") could not recover
deleted objects. Switching to Undo.DestroyObjectImmediate restores the
expected editor-undo behavior with full reference-graph restoration.

The package already uses Undo.DestroyObjectImmediate correctly elsewhere
(ComponentOps.cs, GameObjectComponentHelpers.cs); GameObjectDelete.cs was
the outlier. The stale TODO/comment about "Unity 2022+" is from the
2021.3 era — Undo.DestroyObjectImmediate has been stable across all
currently-supported Unity versions.

Adds Delete_RegistersUndo_RestoresOnUndo test (delete then undo, assert
restoration) to the existing ManageGameObjectDeleteTests baseline.
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 18, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: f15b3eb5-5433-4ff6-ae1b-dd6ae2655434

📥 Commits

Reviewing files that changed from the base of the PR and between 4c29526 and 9973855.

📒 Files selected for processing (2)
  • MCPForUnity/Editor/Tools/GameObjects/GameObjectDelete.cs
  • TestProjects/UnityMCPTests/Assets/Tests/EditMode/Tools/ManageGameObjectDeleteTests.cs

📝 Walkthrough

Walkthrough

This PR enables undo/redo support for GameObject deletion by switching from Object.DestroyImmediate to Undo.DestroyObjectImmediate in the GameObjectDelete handler. A test validates that deleted GameObjects are restored when undo is performed.

Changes

Undo-compatible GameObject deletion

Layer / File(s) Summary
Undo-aware deletion implementation
MCPForUnity/Editor/Tools/GameObjects/GameObjectDelete.cs
GameObjectDelete.Handle replaces Object.DestroyImmediate(targetGo) with Undo.DestroyObjectImmediate(targetGo), removing the previous comment about test unreliability.
Undo behavior verification test
TestProjects/UnityMCPTests/Assets/Tests/EditMode/Tools/ManageGameObjectDeleteTests.cs
New Delete_RegistersUndo_RestoresOnUndo test verifies deletion registers with Unity's Undo system, confirms the object is destroyed, performs undo, and asserts the GameObject is restored.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 A rabbit hops through undo's gleaming thread,
Where GameObjects once gone now live again—
DestroyImmediate? Nay, let Undo spread,
Each deletion blessed with a second when.
Tests hop along to prove it all just right! 🎯

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title directly and clearly describes the main change: switching from Object.DestroyImmediate to Undo.DestroyObjectImmediate to register undo when deleting GameObjects via the manage_gameobject function.
Description check ✅ Passed The description covers all required template sections: Summary, Type of Change (Bug fix), Changes Made, Testing, Related Issues, and notes. It provides detailed context, implementation rationale, test verification, and scoping decisions.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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 and usage tips.

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