Unity-Technologies/UnityCsReference · error · InvalidOperationException
The input 'instanceRoot' was destroyed in the prefabInstance
Error message
The input 'instanceRoot' was destroyed in the prefabInstanceUnpacking callback
What it means
PrefabUtility.UnpackPrefabInstanceAndReturnNewOutermostRoots throws an InvalidOperationException when the instanceRoot GameObject was destroyed during the prefabInstanceUnpacking callback. A subscriber to the prefabInstanceUnpacking event deleted the object, making the unpack operation impossible.
Source
Thrown at Editor/Mono/Prefabs/PrefabUtility.cs:2805
else
{
UnpackPrefabInstanceAndReturnNewOutermostRoots(instanceRoot, unpackMode);
}
if (action == InteractionMode.UserAction)
Undo.FlushTrackedObjects();
}
public static GameObject[] UnpackPrefabInstanceAndReturnNewOutermostRoots(GameObject instanceRoot, PrefabUnpackMode unpackMode)
{
if (instanceRoot == null)
throw new ArgumentNullException(nameof(instanceRoot));
prefabInstanceUnpacking?.Invoke(instanceRoot, unpackMode);
// The user can delete the instance in the prefabInstanceUnpacking callback
if (instanceRoot == null)
throw new InvalidOperationException($"The input '{nameof(instanceRoot)}' was destroyed in the prefabInstanceUnpacking callback");
var newRoots = UnpackPrefabInstanceAndReturnNewOutermostRoots_internal(instanceRoot, unpackMode);
prefabInstanceUnpacked?.Invoke(instanceRoot, unpackMode);
return newRoots;
}
public static void UnpackAllInstancesOfPrefab(GameObject prefabRoot, PrefabUnpackMode unpackMode, InteractionMode action)
{
var prefabInstances = FindAllInstancesOfPrefab(prefabRoot);
foreach (var prefabInstance in prefabInstances)
{
UnpackPrefabInstance(prefabInstance, unpackMode, action);
}
}
internal static List<GameObject> FindGameObjectsWithInvalidComponent(GameObject rootOfSearch)
{View on GitHub (pinned to 225b0fbdb5)
Solutions
- Audit all subscribers to PrefabUtility.prefabInstanceUnpacking and ensure none destroy instanceRoot.
- If you need to clean up related objects, do so after the unpack completes (subscribe to prefabInstanceUnpacked instead).
- Use Object.Destroy (deferred) instead of Object.DestroyImmediate in prefab event callbacks.
- Wrap the UnpackPrefabInstance call in try-catch for InvalidOperationException and log which callback caused the destruction.
Example fix
// before
PrefabUtility.prefabInstanceUnpacking += (root, mode) =>
{
// BUG: this destroys the instance mid-operation
if (someCondition) Object.DestroyImmediate(root);
};
// after
PrefabUtility.prefabInstanceUnpacking += (root, mode) =>
{
// Do NOT destroy root here; defer or handle in unpacked callback
_pendingCleanup.Add(root);
};
PrefabUtility.prefabInstanceUnpacked += (root, mode) =>
{
// Safe to clean up related objects now
}; Defensive patterns
Strategy: try-catch
Validate before calling
// Cannot validate via pre-check; the destruction happens in a callback at runtime. // Audit subscribers to PrefabUtility.prefabInstanceUnpacking.
Try / catch
try
{
PrefabUtility.UnpackPrefabInstanceAndReturnNewOutermostRoots(instanceRoot, mode);
}
catch (InvalidOperationException ex) when (ex.Message.Contains("prefabInstanceUnpacking"))
{
Debug.LogError($"InstanceRoot destroyed during unpacking callback: {ex.Message}");
// Audit and fix the subscriber that destroyed the object
} Prevention
- Never call DestroyImmediate on instanceRoot in prefabInstanceUnpacking callbacks
- Audit all event subscribers for destructive side effects
- Use prefabInstanceUnpacked for cleanup tasks
- Use deferred Destroy instead of DestroyImmediate in prefab callbacks
When it happens
Trigger: An event handler subscribed to PrefabUtility.prefabInstanceUnpacking destroys or destroys the instanceRoot GameObject during the callback. After the callback returns, the method checks if instanceRoot survived and throws if it did not.
Common situations: Custom editor code subscribes to prefabInstanceUnpacking for logging or cleanup and inadvertently destroys the instance (e.g., calling DestroyImmediate, or triggering a scene reload). Also happens with plugins/asset store tools that hook into prefab events aggressively.
Related errors
- plainGameObjects
- plainGameObjects has no objects
- plainGameObjects has a null GameObject
- GameObject is null.
- instanceRoot
AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13).
Data as JSON: /api/errors/3c65454c8ef4f0e3.
Report an issue: GitHub.