{"record":{"id":"152da68d118cdf48","repo":"Unity-Technologies/UnityCsReference","slug":"input-array-contains-null-elements","errorCode":null,"errorMessage":"Input array contains null elements.","messagePattern":"Input array contains null elements\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.cs","lineNumber":388,"sourceCode":"            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\n        internal static void RemoveAllPrefabInstancesUnusedOverridesFromSceneForMenuItem(object userData)\n        {\n            RemoveAllPrefabInstancesUnusedOverridesFromScene((Scene)userData);\n        }\n\n        static void RemoveAllPrefabInstancesUnusedOverridesFromScene(Scene scene)","sourceCodeStart":370,"sourceCodeEnd":406,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.cs#L370-L406","documentation":"Thrown by RemoveUnusedOverrides when the prefabInstances array is non-null but contains one or more null elements. The loop dereferences gameObject before calling IsPartOfPrefabInstance, so a destroyed/missing UnityEngine.Object reference triggers ArgumentException('Input array contains null elements.').","triggerScenarios":"Passing an array built from Selection.gameObjects after some objects were destroyed; arrays holding references to deleted GameObjects; arrays assembled from a dictionary whose values were culled mid-frame.","commonSituations":"Editor automation that collects prefab roots and one gets destroyed before the call runs; selection arrays retained across a scene close that nulled some entries; pooled arrays reused without clearing destroyed refs.","solutions":["Filter nulls before the call: prefabInstances = prefabInstances.Where(go => go != null).ToArray();","Use the Unity null-aware check (go == null catches destroyed Objects) when filtering.","Rebuild the array from fresh PrefabUtility.GetOutermostPrefabInstanceRoot calls right before invoking."],"exampleFix":"// before\nPrefabUtility.RemoveUnusedOverrides(selected, InteractionMode.UserAction);\n\n// after\nvar clean = selected.Where(go => go != null).ToArray();\nif (clean.Length == 0) return;\nPrefabUtility.RemoveUnusedOverrides(clean, InteractionMode.UserAction);","handlingStrategy":"validation","validationCode":"var clean = prefabInstances.Where(go => go != null).ToArray();\nif (clean.Length == 0) return;\nPrefabUtility.RemoveUnusedOverrides(clean, action);","typeGuard":"static GameObject[] FilterLiveInstances(GameObject[] arr)\n    => (arr ?? Array.Empty<GameObject>()).Where(go => go != null).ToArray();","tryCatchPattern":null,"preventionTips":["Use the Unity null check (go == null) which also catches destroyed UnityEngine.Object refs.","Rebuild arrays from fresh selections right before the call rather than caching.","Filter out destroyed/null entries after any operation that could cull them."],"tags":["prefab","validation","null-element"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}