{"record":{"id":"0c8039378fd9acc5","repo":"Unity-Technologies/UnityCsReference","slug":"prefabinstance","errorCode":null,"errorMessage":"prefabInstance","messagePattern":"prefabInstance","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabOverrides/PrefabOverridesUtility.cs","lineNumber":22,"sourceCode":"\nusing System;\nusing System.Collections.Generic;\nusing UnityEngine;\nusing Unity.Scripting.LifecycleManagement;\n\nnamespace UnityEditor.SceneManagement\n{\n    internal class PrefabOverridesUtility\n    {\n        [NoAutoStaticsCleanup] // Reusable scratch buffer, always Clear()ed before and after use; holds no live references across reload.\n        static List<Component> s_ComponentList = new List<Component>();\n        [NoAutoStaticsCleanup] // Reusable scratch buffer, always Clear()ed before and after use; holds no live references across reload.\n        static List<Component> s_AssetComponentList = new List<Component>();\n\n        static void ThrowExceptionIfNullOrNotPartOfPrefabInstance(GameObject prefabInstance)\n        {\n            if (prefabInstance == null)\n                throw new ArgumentNullException(nameof(prefabInstance));\n\n            if (!PrefabUtility.IsPartOfPrefabInstance(prefabInstance))\n                throw new ArgumentException(\"Provided GameObject is not a Prefab instance\");\n        }\n\n        public static List<ObjectOverride> GetObjectOverrides(GameObject prefabInstance, bool includeDefaultOverrides = false)\n        {\n            ThrowExceptionIfNullOrNotPartOfPrefabInstance(prefabInstance);\n\n            var prefabInstanceRoot = PrefabUtility.GetOutermostPrefabInstanceRoot(prefabInstance);\n\n            // From root of instance traverse all child go and detect any GameObjects or components\n            // that are not part of that source prefab objects component list (these must be added)\n            TransformVisitor transformVisitor = new TransformVisitor();\n            var modifiedObjects = new List<ObjectOverride>();\n\n            Func<Transform, object, bool> checkMethod;\n            if (includeDefaultOverrides)","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabOverrides/PrefabOverridesUtility.cs#L4-L40","documentation":"PrefabOverridesUtility.ThrowExceptionIfNullOrNotPartOfPrefabInstance is the precondition guard called by GetObjectOverrides and related methods. It throws ArgumentNullException(nameof(prefabInstance)) when the prefabInstance GameObject is null. This ensures downstream code that traverses transforms and components never dereferences a null reference.","triggerScenarios":"Calling PrefabOverridesUtility.GetObjectOverrides(null) or any other public method that delegates to ThrowExceptionIfNullOrNotPartOfPrefabInstance. Passing a variable that was never assigned or was set to null by a failed lookup (e.g., a GameObject.Find result that returned null).","commonSituations":"Editor extensions that enumerate prefab overrides but receive a null from a failed GameObject lookup. Code that iterates over a collection of GameObjects where some entries are null. Programmatic scene traversal that encounters destroyed objects.","solutions":["Add a null check before calling GetObjectOverrides: if (prefabInstance != null)","Trace the source of the null value (e.g., GameObject.Find, Selection.activeGameObject) and validate the lookup succeeded","Use the ? operator or early return pattern to skip null entries in batch operations"],"exampleFix":"// before\nvar overrides = PrefabOverridesUtility.GetObjectOverrides(selectedGo);\n\n// after\nif (selectedGo == null)\n{\n    Debug.LogWarning(\"No GameObject selected.\");\n    return;\n}\nvar overrides = PrefabOverridesUtility.GetObjectOverrides(selectedGo);","handlingStrategy":"validation","validationCode":"if (prefabInstance == null)\n{\n    Debug.LogWarning(\"prefabInstance is null.\");\n    return;\n}\n// Safe to call GetObjectOverrides(prefabInstance)","typeGuard":"static bool IsValidPrefabInstanceArg(GameObject go) => go != null && PrefabUtility.IsPartOfPrefabInstance(go);","tryCatchPattern":"try { var overrides = PrefabOverridesUtility.GetObjectOverrides(go); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"prefabInstance\")\n{ Debug.LogWarning(\"GameObject was null.\"); }","preventionTips":["Null-check all GameObject arguments before calling prefab override utilities","Validate the source of the GameObject (Selection, Find, traversal) returned a non-null value","Use early-return patterns in batch processing to skip nulls"],"tags":["unity","editor-scripting","prefab","validation","nulcheck","argumentnullexception"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}