Unity-Technologies/UnityCsReference · error · ArgumentNullException
prefabInstance
Error message
prefabInstance
What it means
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.
Source
Thrown at Editor/Mono/Prefabs/PrefabOverrides/PrefabOverridesUtility.cs:22
using System;
using System.Collections.Generic;
using UnityEngine;
using Unity.Scripting.LifecycleManagement;
namespace UnityEditor.SceneManagement
{
internal class PrefabOverridesUtility
{
[NoAutoStaticsCleanup] // Reusable scratch buffer, always Clear()ed before and after use; holds no live references across reload.
static List<Component> s_ComponentList = new List<Component>();
[NoAutoStaticsCleanup] // Reusable scratch buffer, always Clear()ed before and after use; holds no live references across reload.
static List<Component> s_AssetComponentList = new List<Component>();
static void ThrowExceptionIfNullOrNotPartOfPrefabInstance(GameObject prefabInstance)
{
if (prefabInstance == null)
throw new ArgumentNullException(nameof(prefabInstance));
if (!PrefabUtility.IsPartOfPrefabInstance(prefabInstance))
throw new ArgumentException("Provided GameObject is not a Prefab instance");
}
public static List<ObjectOverride> GetObjectOverrides(GameObject prefabInstance, bool includeDefaultOverrides = false)
{
ThrowExceptionIfNullOrNotPartOfPrefabInstance(prefabInstance);
var prefabInstanceRoot = PrefabUtility.GetOutermostPrefabInstanceRoot(prefabInstance);
// From root of instance traverse all child go and detect any GameObjects or components
// that are not part of that source prefab objects component list (these must be added)
TransformVisitor transformVisitor = new TransformVisitor();
var modifiedObjects = new List<ObjectOverride>();
Func<Transform, object, bool> checkMethod;
if (includeDefaultOverrides)View on GitHub (pinned to 225b0fbdb5)
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
Example fix
// before
var overrides = PrefabOverridesUtility.GetObjectOverrides(selectedGo);
// after
if (selectedGo == null)
{
Debug.LogWarning("No GameObject selected.");
return;
}
var overrides = PrefabOverridesUtility.GetObjectOverrides(selectedGo); Defensive patterns
Strategy: validation
Validate before calling
if (prefabInstance == null)
{
Debug.LogWarning("prefabInstance is null.");
return;
}
// Safe to call GetObjectOverrides(prefabInstance) Type guard
static bool IsValidPrefabInstanceArg(GameObject go) => go != null && PrefabUtility.IsPartOfPrefabInstance(go);
Try / catch
try { var overrides = PrefabOverridesUtility.GetObjectOverrides(go); }
catch (ArgumentNullException ex) when (ex.ParamName == "prefabInstance")
{ Debug.LogWarning("GameObject was null."); } Prevention
- 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
When it happens
Trigger: 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).
Common situations: 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.
Related errors
- guid
- The inputObject is null
- Provided GameObject is not a Prefab instance
- Given input object is not a prefab asset
- Object to create variant from has to be a Prefab root
AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13).
Data as JSON: /api/errors/0c8039378fd9acc5.
Report an issue: GitHub.