Search Issue Tracker
By Design
Votes
0
Found in
2021.3.16f1
2022.2.2f1
2023.1.0a25
2023.2.0a2
Issue ID
UUM-28014
Regression
No
Reference to audio clip is lost when Editor loses focus
Reproduction steps:
1. Open the “CatsronautEdited” project
2. Open the “Assets/GAME/Scenes/New Scene.unity” Scene
3. In the Hierarchy Window select “GameObject (1)” GameObject’s child “GameObject” GameObject
4. Enter the Play Mode
5. Observe that an audio clip is assigned in the “AudioClip“ property of the “Audio Source” Component
6. Select another application or switch windows
7. Return to the Editor
Expected results: The audio clip is still playing
Actual results: Reference in the “Audio Clip” property is lost
Reproducible with: 2021.3.16f1, 2022.2.2f1, 2023.1.0a25
, 2023.2.0a2
Could not test with: 2020.3.44f1 (error CS8124: Tuple must contain at least two elements, error CS1526: A new expression requires (), [], or {} after type)
Reproduced on: Intel MacOS 13.1
Note: The audio clip stops playing when the Editor loses focus, but the reference disappears only when returning to Editor
Add comment
All about bugs
View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.
Latest issues
- “Remove Unused Overrides” available on not loaded Scene and throws “ArgumentException: The scene is not loaded” warning
- Adaptive Probe Volume occlusion edge is calculated incorrectly when viewing probes near geometry edges
- Sampling a texture using an HLSL file throws shader errors and the code does not compile
- "Graphics.CopyTexture called with null source texture" error when Base Camera of an Overlay Camera is removed with DX11 Graphics API and Compatibility Mode enabled
- WebGL sends wrong value with large numbers when SendMessage function is used
Resolution Note:
Looking into the repro project I can see that MusicManager.Update is setting the clip to null on line 100, so the clip being null is expected, and is down to the user supplied script.
I would guess the misunderstanding here is the behavior of AudioSource.isPlaying, which is being used as a way to tell if a clip has stopped. Under normal circumstances this will work - however when unity is put into background the audio system will suspend, causing isPlaying to return false. isPlaying works much more like “is sound coming out of the speakers right now”, which is not the case when the system is suspended. On this point we should update the documentation to make this more clear.
I can see two solutions here, the first is pretty easy. If you are happy to keep the music playing whilst unity is not in focus, set run in background in Edit > Project Settings -> Player>Resolution->Run In Background to true.
The other solution to be to use AudioSource.time with AudioClip.Length to check if the source has actually finished playing, and do the source swapping logic when that is the case.