{"record":{"id":"6755a6cbffa1c0ee","repo":"Unity-Technologies/UnityCsReference","slug":"prefabinstanceroots","errorCode":null,"errorMessage":"prefabInstanceRoots","messagePattern":"prefabInstanceRoots","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.cs","lineNumber":2283,"sourceCode":"\n            if (mode == InteractionMode.UserAction)\n            {\n                // Recording undo does not handle missing scripts\n                var gameObjectsWithInvalidScript = FindGameObjectsWithInvalidComponent(prefabInstanceRoot);\n                if (gameObjectsWithInvalidScript.Count > 0)\n                    throw new InvalidOperationException(string.Format($\"Cannot replace the Prefab instance when it has a missing script. GameObject '{gameObjectsWithInvalidScript[0].name}' has a missing script. Use InteractionMode.AutomatedAction to force the replace.\"));\n            }\n        }\n\n        public static void ReplacePrefabAssetOfPrefabInstances(GameObject[] prefabInstanceRoots, GameObject prefabAssetRoot, InteractionMode mode)\n        {\n            ReplacePrefabAssetOfPrefabInstances(prefabInstanceRoots, prefabAssetRoot, new PrefabReplacingSettings(), mode);\n        }\n\n        public static void ReplacePrefabAssetOfPrefabInstances(GameObject[] prefabInstanceRoots, GameObject prefabAssetRoot, PrefabReplacingSettings settings, InteractionMode mode)\n        {\n            if (prefabInstanceRoots == null)\n                throw new ArgumentNullException(nameof(prefabInstanceRoots));\n\n            if (prefabInstanceRoots.Length == 0)\n                throw new ArgumentException(nameof(prefabInstanceRoots) + \" has no objects\");\n\n            if (settings == null)\n                throw new ArgumentNullException(nameof(settings));\n\n            ThrowIfInvalidAssetForReplacePrefabInstance(prefabAssetRoot, mode);\n            foreach (var go in prefabInstanceRoots)\n                ThrowIfInvalidArgumentsForReplacePrefabInstance(go, prefabAssetRoot, false, mode);\n\n            foreach (var go in prefabInstanceRoots)\n                ReplacePrefabAssetOfPrefabInstance_NoInputValidation(go, prefabAssetRoot, settings, mode);\n\n            EditorUtility.ForceRebuildInspectors();\n        }\n\n        public static void ReplacePrefabAssetOfPrefabInstance(GameObject prefabInstanceRoot, GameObject prefabAssetRoot, InteractionMode mode)","sourceCodeStart":2265,"sourceCodeEnd":2301,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.cs#L2265-L2301","documentation":"Thrown by ReplacePrefabAssetOfPrefabInstances when the prefabInstanceRoots array is null. This is a standard ArgumentNullException guard on the first parameter of the batch replace API. The nameof(prefabInstanceRoots) string is used as the argument name in the exception.","triggerScenarios":"Calling ReplacePrefabAssetOfPrefabInstances(null, prefabAsset, mode) — e.g. a variable that was never assigned, or a LINQ result that returned null on an empty source.","commonSituations":"Selection.gameObjects passed without null-checking when nothing is selected. Deserialized data that failed to populate the array. Code path where the array is conditionally populated but the condition was false.","solutions":["Null-check the array before calling the API.","Initialize the array to an empty array instead of leaving it null.","Use Selection.gameObjects?.Where(...) ?? Array.Empty<GameObject>() to guarantee non-null."],"exampleFix":"// before\nPrefabUtility.ReplacePrefabAssetOfPrefabInstances(myArray, prefabAsset, mode);\n// myArray could be null\n\n// after\nif (myArray != null && myArray.Length > 0)\n    PrefabUtility.ReplacePrefabAssetOfPrefabInstances(myArray, prefabAsset, mode);","handlingStrategy":"validation","validationCode":"if (prefabInstanceRoots == null)\n{\n    Debug.LogError(\"prefabInstanceRoots is null.\");\n    return;\n}\nPrefabUtility.ReplacePrefabAssetOfPrefabInstances(prefabInstanceRoots, prefabAssetRoot, mode);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Initialize arrays to Array.Empty<GameObject>() instead of null.","Use null-coalescing when building arrays from filters: (Selection.gameObjects ?? Array.Empty<GameObject>())."],"tags":["prefab","argument-null","argument-validation","unity-editor"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}