Unity-Technologies/UnityCsReference · error · ArgumentNullException
container
Error message
container
What it means
ArgumentNullException thrown by ValidateContainer when the container parameter is null. This is the first guard in the validation chain for all AudioRandomContainer extension methods, ensuring the target container is a valid object before any operations are performed on it.
Source
Thrown at Editor/Mono/Audio/AudioRandomContainerExtensions.cs:55
ValidateContainer(container);
if (clips == null)
throw new ArgumentNullException(nameof(clips));
if (clips.Length == 0)
throw new ArgumentException("Must not be empty.", nameof(clips));
AddElementsInner(container, clips.Length, (i, element) =>
{
if (clips[i] != null)
element.audioClip = clips[i];
});
}
static void ValidateContainer(AudioRandomContainer container)
{
if (container == null)
throw new ArgumentNullException(nameof(container));
if (!EditorUtility.IsPersistent(container))
throw new ArgumentException("Must be a persistent asset.", nameof(container));
}
static void AddElementsInner(AudioRandomContainer container, int count,
Action<int, AudioContainerElement> configureElement)
{
var undoGroupName = count > 1 ? $"{k_BaseAddElementsUndoName}s" : k_BaseAddElementsUndoName;
Undo.RegisterCompleteObjectUndo(container, undoGroupName);
Undo.SetCurrentGroupName(undoGroupName);
if (container.elements == null)
container.elements = Array.Empty<AudioContainerElement>();
var newElements = new AudioContainerElement[count];
var oldAndNewElements = new AudioContainerElement[container.elements.Length + count];View on GitHub (pinned to 225b0fbdb5)
Solutions
- Null-check the container before calling AddElements or any extension method.
- Ensure the AudioRandomContainer asset is assigned in the inspector or created before use.
- If using SerializedObject, verify targetObject is not null and is of the correct type before casting.
- Add a Debug.LogError or early-return when the container reference is missing to aid debugging.
Example fix
// before
container.AddElements(clips); // container may be null
// after
if (container == null)
{
Debug.LogError("AudioRandomContainer is not assigned.");
return;
}
container.AddElements(clips); Defensive patterns
Strategy: type-guard
Validate before calling
// Null-check container before calling any extension method
if (container != null)
container.AddElements(clips); Type guard
static bool IsValidContainer(AudioRandomContainer container) => container != null;
Prevention
- Always null-check AudioRandomContainer references before calling extension methods.
- Verify SerializedObject.targetObject is not null before casting to AudioRandomContainer.
- Guard against destroyed or unloaded assets with null checks.
- Use [SerializeField] with [NotNull] attributes or initialization to reduce null risk.
When it happens
Trigger: Calling any AddElements overload (or any method using ValidateContainer) with a null AudioRandomContainer. Occurs when a SerializedObject.targetObject cast fails, when a script references a container that was destroyed, or when a field is unassigned.
Common situations: Editor windows with unassigned container references, scripts that destroy or null-out a container before a deferred AddElements call, deserialized assets where the container reference is missing, component lookups that return null.
Related errors
AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13).
Data as JSON: /api/errors/221ea9673563a29d.
Report an issue: GitHub.