{"record":{"id":"8069d1737dcd13fc","repo":"Unity-Technologies/UnityCsReference","slug":"prefabinstances","errorCode":null,"errorMessage":"prefabInstances","messagePattern":"prefabInstances","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.cs","lineNumber":382,"sourceCode":"            {\n                foreach (var instanceRoot in instanceRoots)\n                {\n                    ApplyPrefabInstance(instanceRoot, action, undoFlushTrackedObjects:false);\n                }\n            }\n            finally\n            {\n                AssetDatabase.StopAssetEditing();\n            }\n\n            if (action == InteractionMode.UserAction)\n                Undo.FlushTrackedObjects(); // Needs to be called after StopAssetEditing() to fix UUM-6917\n        }\n\n        public static void RemoveUnusedOverrides(GameObject[] prefabInstances, InteractionMode action)\n        {\n            if (prefabInstances == null)\n                throw new ArgumentNullException(nameof(prefabInstances));\n\n            HashSet<GameObject> rootSet = new HashSet<GameObject>(prefabInstances.Length);\n            foreach (var gameObject in prefabInstances)\n            {\n                if (gameObject == null)\n                    throw new ArgumentException(\"Input array contains null elements.\", nameof(prefabInstances));\n                if (!PrefabUtility.IsPartOfPrefabInstance(gameObject))\n                    throw new ArgumentException(\"Input array contains objects which are not part of a Prefab instance.\", nameof(prefabInstances));\n\n                rootSet.Add(PrefabUtility.GetOutermostPrefabInstanceRoot(gameObject));\n            }\n\n#pragma warning disable UA2001 // The Banned API Analyzer produces compile errors for any new Linq code. This pre-existing usage has been suppressed, but should be rewritten if possible.\n            InstanceOverridesInfo[] instanceOverridesInfos = rootSet.Select(PrefabUtility.GetPrefabInstanceOverridesInfo_Internal).ToArray();\n#pragma warning restore UA2001\n            PrefabUtility.RemovePrefabInstanceUnusedOverrides(instanceOverridesInfos, action);\n        }\n","sourceCodeStart":364,"sourceCodeEnd":400,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.cs#L364-L400","documentation":"Thrown by RemoveUnusedOverrides(GameObject[] prefabInstances, InteractionMode) when prefabInstances is null. The method allocates a HashSet sized by prefabInstances.Length and enumerates it, so null fails immediately with ArgumentNullException(nameof(prefabInstances)).","triggerScenarios":"Calling PrefabUtility.RemoveUnusedOverrides(null, action); passing a filtered selection array that ended up null; passing a field that was never populated.","commonSituations":"Cleanup-on-save editor scripts operating on a null selection; menu items invoked with no prefab instance selected; arrays sourced from a query that returned null instead of empty.","solutions":["Coalesce to an empty array: RemoveUnusedOverrides(prefabInstances ?? Array.Empty<GameObject>(), action).","Early-return when no prefab instances are selected: if (prefabInstances == null || prefabInstances.Length == 0) return;","Build the array from a validated selection and guarantee non-null."],"exampleFix":"// before\nPrefabUtility.RemoveUnusedOverrides(GetSelectedInstances(), InteractionMode.UserAction);\n\n// after\nvar instances = GetSelectedInstances() ?? Array.Empty<GameObject>();\nPrefabUtility.RemoveUnusedOverrides(instances, InteractionMode.UserAction);","handlingStrategy":"validation","validationCode":"prefabInstances = prefabInstances ?? Array.Empty<GameObject>();\nif (prefabInstances.Length == 0) return;\nPrefabUtility.RemoveUnusedOverrides(prefabInstances, action);","typeGuard":"static bool IsNonNullableInstanceArray(GameObject[] arr)\n    => arr != null && arr.All(go => go != null);","tryCatchPattern":null,"preventionTips":["Never pass a null array to RemoveUnusedOverrides; pass an empty array instead.","Initialize array fields before conditional population.","Wrap selection-derived arrays with a non-null guarantee."],"tags":["prefab","validation","null-argument"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}