Unity Issue Tracker - Crash on purecall when an audio source is trying to play a destroyed clip
Fixed
UUM-14330
Crash on purecall when an audio source is trying to play a destroyed clip
Unity: Audio Authoring
Fixed
6.0.X
2023.2.X
2021.3.X
Won't Fix
2023.1.X
2022.2.X
2022.1.X
Audio: Fixed an issue that would cause Unity to crash when you manually destroy a streamed audio clip.
2022.1 has reached end of life so the issue will be fixed in a newer version.
Reproduction steps:
Open the attached project "Download Audio Crash".
Enter play mode.
Press spacebar.
Press backspace.
Expected: The editor doesn't crash and the editor throws an error. If using DestroyImmediate, the audio source should stop outputting sound.
Actual: The audio source continues outputting sound for a destroyed audio clip and the editor crashes after a few seconds.
Reproducible on: 2021.3.35f1, 2022.3.20f1, 2023.2.13f1, 2023.3.0b10
Reproduced on: Windows 10, Windows 11, MacOS (M1)
Note
In the attached project "Download Audio Crash", you can change the Using Resources Load boolean to true on the game object script to test what happens if using that method instead.
This error happens when using Resources.Load instead, and could be the expected results. However, the user can use DestroyImmediate to repro the error, so there should be a safe guard in place where the audio source is trying to play a clip that has been deleted.
Expected error:
Destroying assets is not permitted to avoid data loss.
If you really want to remove an asset use DestroyImmediate (theObject, true);
UnityEngine.Object:Destroy (UnityEngine.Object)
StreamAudio:Update () (at Assets/StreamAudio.cs:27)
If using Resources.Load and DestroyImmediate, the audio source actually stops outputting sound immediately when the clip is destroyed.