{"record":{"id":"ea54399ce38c70e4","repo":"Unity-Technologies/UnityCsReference","slug":"must-be-a-persistent-asset","errorCode":null,"errorMessage":"Must be a persistent asset.","messagePattern":"Must be a persistent asset\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Audio/AudioRandomContainerExtensions.cs","lineNumber":58,"sourceCode":"            throw new ArgumentNullException(nameof(clips));\n\n        if (clips.Length == 0)\n            throw new ArgumentException(\"Must not be empty.\", nameof(clips));\n\n        AddElementsInner(container, clips.Length, (i, element) =>\n        {\n            if (clips[i] != null)\n                element.audioClip = clips[i];\n        });\n    }\n\n    static void ValidateContainer(AudioRandomContainer container)\n    {\n        if (container == null)\n            throw new ArgumentNullException(nameof(container));\n\n        if (!EditorUtility.IsPersistent(container))\n            throw new ArgumentException(\"Must be a persistent asset.\", nameof(container));\n    }\n\n    static void AddElementsInner(AudioRandomContainer container, int count,\n        Action<int, AudioContainerElement> configureElement)\n    {\n        var undoGroupName = count > 1 ? $\"{k_BaseAddElementsUndoName}s\" : k_BaseAddElementsUndoName;\n\n        Undo.RegisterCompleteObjectUndo(container, undoGroupName);\n        Undo.SetCurrentGroupName(undoGroupName);\n\n        if (container.elements == null)\n            container.elements = Array.Empty<AudioContainerElement>();\n\n        var newElements = new AudioContainerElement[count];\n        var oldAndNewElements = new AudioContainerElement[container.elements.Length + count];\n        Array.Copy(container.elements, oldAndNewElements, container.elements.Length);\n\n        for (var i = 0; i < count; i++)","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Audio/AudioRandomContainerExtensions.cs#L40-L76","documentation":"ArgumentException thrown by ValidateContainer when EditorUtility.IsPersistent(container) returns false. This guard ensures the AudioRandomContainer is a saved asset (e.g. a .asset file in the project) rather than a scene-bound or transient instance, because the undo and element-adding operations rely on asset persistence.","triggerScenarios":"Calling AddElements on an AudioRandomContainer that exists only in a scene (not saved as a project asset), or on a runtime-instantiated container. EditorUtility.IsPersistent returns false for scene objects and ephemeral instances.","commonSituations":"Attempting to edit a container that lives in a scene instead of as a ScriptableObject asset, working with runtime-created instances in editor tooling, prefab-embedded containers that are not yet saved as assets, mistaken assumptions about where the container resides.","solutions":["Ensure the AudioRandomContainer is saved as a project asset (Create > Audio > Audio Random Container or via AssetDatabase.CreateAsset).","If the container is scene-based, refactor it to be a persistent asset referenced by the scene.","Verify with EditorUtility.IsPersistent(container) before calling AddElements.","Check that AssetDatabase.Contains(container) returns true for the target."],"exampleFix":"// before — container is a scene instance\ncontainer.AddElements(clips);\n// after — verify persistence first\nif (!EditorUtility.IsPersistent(container))\n{\n    Debug.LogError(\"AudioRandomContainer must be a saved asset, not a scene instance.\");\n    return;\n}\ncontainer.AddElements(clips);","handlingStrategy":"validation","validationCode":"// Verify container is a persistent asset before calling AddElements\nif (container != null && EditorUtility.IsPersistent(container))\n    container.AddElements(clips);\nelse\n    Debug.LogError(\"AudioRandomContainer must be a saved project asset.\");","typeGuard":"static bool IsPersistentContainer(AudioRandomContainer container)\n    => container != null && EditorUtility.IsPersistent(container);","tryCatchPattern":null,"preventionTips":["Save AudioRandomContainer as a .asset file using AssetDatabase.CreateAsset.","Avoid using scene-bound or runtime-instantiated containers with editor extensions.","Check EditorUtility.IsPersistent before calling container modification methods.","Understand the distinction between scene objects and project assets in Unity editor scripting."],"tags":["audio","editor","validation","asset-persistence","argument-validation","audio-random-container"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}