{"record":{"id":"3eab35a9d089046b","repo":"Unity-Technologies/UnityCsReference","slug":"instanceroots","errorCode":null,"errorMessage":"instanceRoots","messagePattern":"instanceRoots","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.cs","lineNumber":354,"sourceCode":"\n                if (undoFlushTrackedObjects && action == InteractionMode.UserAction)\n                    Undo.FlushTrackedObjects();\n            }\n\n            Analytics.SendApplyEvent(\n                Analytics.ApplyScope.EntirePrefab,\n                instanceRoot,\n                null,\n                action,\n                startTime,\n                false\n            );\n        }\n\n        public static void ApplyPrefabInstances(GameObject[] instanceRoots, InteractionMode action)\n        {\n            if (instanceRoots == null)\n                throw new ArgumentNullException(nameof(instanceRoots));\n\n            foreach (var instanceRoot in instanceRoots)\n            {\n                ThrowExceptionIfNotValidPrefabInstanceObject(instanceRoot, true);\n            }\n\n            // Apply sequentially but import after all input have been saved to disk\n            AssetDatabase.StartAssetEditing();\n            try\n            {\n                foreach (var instanceRoot in instanceRoots)\n                {\n                    ApplyPrefabInstance(instanceRoot, action, undoFlushTrackedObjects:false);\n                }\n            }\n            finally\n            {\n                AssetDatabase.StopAssetEditing();","sourceCodeStart":336,"sourceCodeEnd":372,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.cs#L336-L372","documentation":"Thrown by ApplyPrefabInstances(GameObject[] instanceRoots, InteractionMode) when the array argument is null. The method iterates the array immediately to validate each root, so a null array dereferences before any work begins. Unity uses ArgumentNullException with nameof(instanceRoots) to make the offending parameter explicit.","triggerScenarios":"Calling PrefabUtility.ApplyPrefabInstances(null, action); passing a variable that was never assigned; passing the result of a LINQ/Find operation that returned null because no selection existed.","commonSituations":"Selection.transforms-derived GameObject arrays where nothing is selected; editor scripts that build the array conditionally and leave it null when no instances match; refactors that removed the array population step.","solutions":["Pass an empty array instead of null: ApplyPrefabInstances(Array.Empty<GameObject>(), action).","Initialize the array before conditional population and null-check before the call.","If the array comes from a query, coalesce with ?? Array.Empty<GameObject>()."],"exampleFix":"// before\nvar roots = Selection.transforms.Length > 0 ? Selection.gameObjects : null;\nPrefabUtility.ApplyPrefabInstances(roots, InteractionMode.AutomatedAction);\n\n// after\nvar roots = Selection.gameObjects ?? Array.Empty<GameObject>();\nPrefabUtility.ApplyPrefabInstances(roots, InteractionMode.AutomatedAction);","handlingStrategy":"validation","validationCode":"instanceRoots ??= Array.Empty<GameObject>();\nif (instanceRoots.Length == 0) return;\nPrefabUtility.ApplyPrefabInstances(instanceRoots, action);","typeGuard":"static bool IsUsableRootArray(GameObject[] arr)\n    => arr != null && arr.All(go => go != null && PrefabUtility.IsPartOfPrefabInstance(go));","tryCatchPattern":null,"preventionTips":["Coalesce nullable arrays with ?? Array.Empty<GameObject>() at the call site.","Build arrays from validated selections and guarantee non-null.","Early-return on empty arrays to skip the internal validation loop."],"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"}