{"record":{"id":"78bde7813981d683","repo":"Unity-Technologies/UnityCsReference","slug":"value-cannot-be-null-parameter-instance","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'instance')","messagePattern":"Value cannot be null\\. \\(Parameter 'instance'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.cs","lineNumber":2158,"sourceCode":"            var assetRoot = SaveAsPrefabAssetAndConnect_Internal(instanceRoot, assetPath, out success);\n\n            if (!success)\n            {\n                return null;\n            }\n\n            if (action == InteractionMode.UserAction)\n            {\n                Undo.RecordCreatedObject(GetPrefabInstanceHandle(instanceRoot), actionName);\n            }\n\n            return assetRoot;\n        }\n\n        internal static void ApplyPrefabInstance(GameObject instance)\n        {\n            if (instance == null)\n                throw new ArgumentNullException(nameof(instance));\n\n            // Include model check even though models are also immutable, since we can give a more clear exception message.\n            if (IsPartOfModelPrefab(instance))\n                throw new ArgumentException(\"Can't apply to a Model Prefab\");\n\n            if (IsPartOfImmutablePrefab(instance))\n                throw new ArgumentException(\"Can't apply to an immutable Prefab\");\n\n            if (!IsPartOfNonAssetPrefabInstance(instance))\n                throw new ArgumentException(\"Provided GameObject is not a Prefab instance\");\n\n            var root = GetOutermostPrefabInstanceRoot(instance);\n            if (root != instance)\n                throw new ArgumentException(\"GameObject to save Prefab from must be a Prefab root\");\n\n            var assetObject = GetCorrespondingObjectFromSource(instance);\n            string path = AssetDatabase.GetAssetPath(assetObject);\n","sourceCodeStart":2140,"sourceCodeEnd":2176,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.cs#L2140-L2176","documentation":"Thrown by ApplyPrefabInstance when the instance argument is null. This is the entry guard: applying overrides to a Prefab requires a non-null GameObject reference.","triggerScenarios":"Calling PrefabUtility.ApplyPrefabInstance(null) or passing a null GameObject from a failed lookup.","commonSituations":"Selection.activeGameObject returns null (nothing selected). A GameObject reference became null after Undo or scene load. Editor script runs in a context where no instance is selected.","solutions":["Null-check instance before calling ApplyPrefabInstance.","Verify the selection or lookup that produced the reference is valid in the current editor context.","Guard editor menu item handlers with [MenuItem(...)] validation or ValidateFunction."],"exampleFix":"// before\nPrefabUtility.ApplyPrefabInstance(Selection.activeGameObject);\n// after\nGameObject sel = Selection.activeGameObject;\nif (sel != null)\n    PrefabUtility.ApplyPrefabInstance(sel);","handlingStrategy":"type-guard","validationCode":"if (instance == null)\n{ Debug.LogError(\"Cannot apply: instance is null.\"); return; }\nPrefabUtility.ApplyPrefabInstance(instance);","typeGuard":"static bool IsApplyableInstance(GameObject go) =>\n    go != null && PrefabUtility.IsPartOfNonAssetPrefabInstance(go);","tryCatchPattern":null,"preventionTips":["Validate Selection.activeGameObject is non-null before apply.","Use MenuItem validation functions to disable apply when nothing is selected.","Guard automated pipelines with null checks on every instance reference."],"tags":["unity","prefab","null-reference","apply"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}