From 9c4e74de03e2b255e4a9ba11a20bbd6a941ec81c Mon Sep 17 00:00:00 2001 From: Hyunwook Ha Date: Fri, 20 Jun 2025 09:18:16 +0900 Subject: [PATCH 1/2] fix for AudioController.Update() at 2d game tutorial. --- .../15_audio_controller/snippets/audiocontroller.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/articles/tutorials/building_2d_games/15_audio_controller/snippets/audiocontroller.cs b/articles/tutorials/building_2d_games/15_audio_controller/snippets/audiocontroller.cs index 34b57261..9a504d60 100644 --- a/articles/tutorials/building_2d_games/15_audio_controller/snippets/audiocontroller.cs +++ b/articles/tutorials/building_2d_games/15_audio_controller/snippets/audiocontroller.cs @@ -119,9 +119,9 @@ public void Update() if (instance.State == SoundState.Stopped && !instance.IsDisposed) { instance.Dispose(); + _activeSoundEffectInstances.RemoveAt(index); } - - _activeSoundEffectInstances.RemoveAt(index); + index++; } } #endregion From 35e6e24255f9ac22ea6a32093966efcaa7f51383 Mon Sep 17 00:00:00 2001 From: Hyunwook Ha Date: Wed, 25 Jun 2025 11:45:10 +0900 Subject: [PATCH 2/2] update code with backward iteration --- .../snippets/audiocontroller.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/articles/tutorials/building_2d_games/15_audio_controller/snippets/audiocontroller.cs b/articles/tutorials/building_2d_games/15_audio_controller/snippets/audiocontroller.cs index 9a504d60..686df577 100644 --- a/articles/tutorials/building_2d_games/15_audio_controller/snippets/audiocontroller.cs +++ b/articles/tutorials/building_2d_games/15_audio_controller/snippets/audiocontroller.cs @@ -110,18 +110,18 @@ public AudioController() /// public void Update() { - int index = 0; - - while (index < _activeSoundEffectInstances.Count) + for (int i = _activeSoundEffectInstances.Count - 1; i >= 0; i--) { - SoundEffectInstance instance = _activeSoundEffectInstances[index]; + SoundEffectInstance instance = _activeSoundEffectInstances[i]; - if (instance.State == SoundState.Stopped && !instance.IsDisposed) + if (instance.State == SoundState.Stopped) { - instance.Dispose(); - _activeSoundEffectInstances.RemoveAt(index); + if (!instance.IsDisposed) + { + instance.Dispose(); + } + _activeSoundEffectInstances.RemoveAt(i); } - index++; } } #endregion