Chapter 8 of 15 9 min intermediate

Safety & Git Hygiene

Never let an agent eat your scene

Verified against a working setup on 2026-08-10

An agent editing your Unity project is doing something a normal AI chat cannot: making changes you did not see happen. That is the entire value, and the entire risk. This chapter is the boring part that saves your weekend.

The one rule

Never start an agent session on a dirty working tree. Commit or stash first. If everything is committed, any mistake is one git restore away. If it is not, you are hand-reconstructing a scene file.
bash
# Before every session
git status              # must be clean
git switch -c ai/stamina-system

# After, review honestly
git diff --stat
git diff -- "*.cs"      # read every line of script changes

# If it went wrong
git restore .
git clean -fd           # removes files it created

Why Unity makes this harder

Reviewing a code diff is easy. Reviewing a Unity diff is not, and it is worth understanding why before you trust an agent with scene edits.

  • Scenes and prefabs are YAML full of GUIDs. A one-object change can produce hundreds of diff lines that mean nothing to a human reader.
  • Reordering is invisible in intent. Unity may rewrite unrelated parts of a scene file just from being opened and saved.
  • .meta files matter. Delete or regenerate one and every reference to that asset breaks — often silently, showing up later as a missing script.
  • The Editor holds state in memory. An agent writing files while Unity has unsaved changes can have its work overwritten, or overwrite yours.
Save your scene before an agent session. If Unity has unsaved changes and the agent edits the same scene, whichever writes last wins — and it is not always the one you wanted.

Set up git properly for Unity

If you have not already, two settings make Unity diffs dramatically more reviewable. Both are in Edit → Project Settings → Editor.

SettingSet toWhy
Version Control ModeVisible Meta FilesMeta files are tracked in git, so references survive.
Asset SerializationForce TextScenes become readable YAML instead of binary.
Without Force Text, scenes are binary. Git cannot show a diff at all, so you have literally no way to review what an agent changed in a scene. Turn it on before you let anything near your project.

Tool permissions

Read tools and write tools deserve very different levels of trust. This split holds regardless of which client you use.

ToolsRiskRecommendation
Read console, read scene, searchNoneAlways allow. Most of the value lives here.
Profiler, capture screenshotsNoneAlways allow.
Create or edit scriptsLowFine on a clean branch. Read the diff.
Modify GameObjects or scenesHighAsk every time. Hard to review afterwards.
Delete assets or scriptsHighAsk every time. Meta files break references.
Run arbitrary menu itemsHighestA menu item can do anything, including build or wipe.

Scope your prompts

Most damage comes from an unbounded instruction rather than a malicious one. The model does exactly what you asked across a wider surface than you pictured.

text
In Assets/Scripts/Enemies only, rename the field
"hp" to "currentHealth" and update every reference
within that folder. List each file you changed.

Things to never let it touch

  • Library/ — Unity's generated cache. Editing it corrupts the project. It should be gitignored anyway.
  • ProjectSettings/ — changes here affect physics, layers, and build config globally, and are easy to miss in review.
  • Packages/manifest.json — unless you are deliberately adding a package. A bad edit breaks the whole project on next import.
  • Third-party asset folders — you cannot meaningfully review changes to code you did not write.
If your client supports a project instruction file, put these exclusions in it once rather than repeating them in every prompt.

Recovering

When something has clearly gone wrong

Stop the agent first. Letting it continue while you investigate compounds the problem.

bash
# 1. See the scale of it
git status
git diff --stat

# 2. Nuclear option - back to last commit
git restore .
git clean -fd

# 3. Or keep the good parts
git restore --source=HEAD -- Assets/Scenes/
git diff -- Assets/Scripts/   # review, keep what is right

Then close and reopen Unity. The Editor caches asset state in memory, so after a git restore it can still be showing you the reverted-away version until it reimports.

If you were not on a branch and had not committed

Check your editor's local history first — VS Code, Rider and Visual Studio all keep per-file timelines that survive external changes, and they are often enough to recover a script.

Scenes are harder. Unity keeps no equivalent history. This is precisely why the commit-first rule is the first thing in this chapter rather than the last.

What to take away

  • Commit before every agent session, and work on a branch.
  • Turn on Force Text and Visible Meta Files before anything else.
  • Always allow read tools; keep scene, delete and menu tools on ask.
  • Scope every prompt to a folder or the open scene.
  • Keep it out of Library, ProjectSettings, and third-party folders.
  • Reopen Unity after a git restore so it reimports.