{"record":{"id":"03d5c7c3aa5bff29","repo":"CoplayDev/unity-mcp","slug":"could-not-retrieve-audio-samples","errorCode":null,"errorMessage":"Could not retrieve audio samples","messagePattern":"Could not retrieve audio samples","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"TestProjects/AssetStoreUploads/Packages/com.unity.asset-store-tools/Editor/Previews/Scripts/Generators/Custom/TypeGenerators/AudioTypePreviewGenerator.cs","lineNumber":166,"sourceCode":"\r\n            for (int i = 0; i < audioClip.channels; i++)\r\n            {\r\n                var channelMaxY = (_texture.height - 1) - i * sectionSize;\r\n                var channelMinY = _texture.height - (i + 1) * sectionSize;\r\n                var channel = new AudioChannel(channelMinY, channelMaxY, channelSamples[i]);\r\n                channels.Add(channel);\r\n            }\r\n\r\n            return channels;\r\n        }\r\n\r\n        private List<List<float>> GetChannelSamples(AudioClip audioClip)\r\n        {\r\n            var channelSamples = new List<List<float>>();\r\n            var allSamples = new float[audioClip.samples * audioClip.channels];\r\n\r\n            if (!audioClip.GetData(allSamples, 0))\r\n                throw new Exception(\"Could not retrieve audio samples\");\r\n\r\n            for (int i = 0; i < audioClip.channels; i++)\r\n            {\r\n                var samples = new List<float>();\r\n                var sampleIndex = i;\r\n                while (sampleIndex < allSamples.Length)\r\n                {\r\n                    samples.Add(allSamples[sampleIndex]);\r\n                    sampleIndex += audioClip.channels;\r\n                }\r\n\r\n                channelSamples.Add(samples);\r\n            }\r\n\r\n            return channelSamples;\r\n        }\r\n\r\n        private void DrawChannel(AudioChannel channel)\r","sourceCodeStart":148,"sourceCodeEnd":184,"githubUrl":"https://github.com/CoplayDev/unity-mcp/blob/c21bf496bca87d54e75bad048563c3adb1782081/TestProjects/AssetStoreUploads/Packages/com.unity.asset-store-tools/Editor/Previews/Scripts/Generators/Custom/TypeGenerators/AudioTypePreviewGenerator.cs#L148-L184","documentation":"Thrown when AudioClip.GetData returns false, meaning the raw PCM sample array could not be retrieved from the loaded audio clip. GetData requires the audio data to already be loaded in memory; if it isn't, or if the clip format doesn't support sample access, this call fails.","triggerScenarios":"Calling GetChannelSamples(audioClip) where audioClip.GetData(allSamples, 0) returns false. The sample buffer is pre-allocated as float[clip.samples * clip.channels].","commonSituations":"Audio data was not preloaded (LoadAudioData was called but is async and hasn't finished); clip is a tracker/mod format that doesn't expose PCM samples directly; clip was unloaded between LoadAudioData and GetData; very large clip where the float buffer allocation fails or Unity refuses to fill it.","solutions":["Ensure LoadAudioData() has completed before calling GetData — yield a frame or poll audioClip.loadState until it is Loaded.","Verify the AudioClip supports sample retrieval (some compressed/streaming formats do not).","Handle large clips by checking that samples * channels does not exceed reasonable memory limits before allocating."],"exampleFix":"// before\nif (!audioClip.LoadAudioData())\n    throw new Exception(\"Could not load audio data\");\n// immediately accesses samples\nif (!audioClip.GetData(allSamples, 0))\n    throw new Exception(\"Could not retrieve audio samples\");\n\n// after — wait for load completion\nif (!audioClip.LoadAudioData())\n    throw new Exception(\"Could not load audio data\");\nwhile (audioClip.loadState != AudioDataLoadState.Loaded)\n    await Task.Yield();\nif (!audioClip.GetData(allSamples, 0))\n    throw new Exception(\"Could not retrieve audio samples\");","handlingStrategy":"try-catch","validationCode":"// Ensure data is fully loaded before requesting samples\nif (audioClip.loadState != AudioDataLoadState.Loaded)\n{\n    audioClip.LoadAudioData();\n    while (audioClip.loadState == AudioDataLoadState.Loading)\n        await Task.Yield();\n}\n// Pre-validate buffer size to avoid OOM\nvar sampleCount = audioClip.samples * audioClip.channels;\nif (sampleCount <= 0 || sampleCount > 100_000_000)\n{\n    Debug.LogWarning($\"Skipping {audioClip.name}: invalid or excessive sample count ({sampleCount}).\");\n    continue;\n}","typeGuard":"static bool IsAudioDataAccessible(AudioClip clip)\n    => clip != null && clip.loadState == AudioDataLoadState.Loaded && clip.samples > 0;","tryCatchPattern":"try\n{\n    var samples = GetChannelSamples(audioClip);\n}\ncatch (Exception ex) when (ex.Message.Contains(\"Could not retrieve audio samples\"))\n{\n    ASDebug.LogWarning($\"Skipping {audioClip.name}: sample data unavailable.\");\n    continue;\n}","preventionTips":["Always poll audioClip.loadState until Loaded before calling GetData.","Ensure LoadAudioData() was called and completed before sample retrieval.","Filter out streaming/compressed clips that don't support direct sample access.","Validate sample buffer size before allocation to avoid OOM on very large clips."],"tags":["unity","asset-store-tools","preview","audio","asset-loading"],"backgroundTag":null,"analyzedSha":"c21bf496bca87d54e75bad048563c3adb1782081","analyzedAt":"2026-08-13T17:36:56.095Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}