diff --git a/Assets/Scripts/Behaviours/Vehicles/Vehicle.cs b/Assets/Scripts/Behaviours/Vehicles/Vehicle.cs index 269acecb..4248001d 100644 --- a/Assets/Scripts/Behaviours/Vehicles/Vehicle.cs +++ b/Assets/Scripts/Behaviours/Vehicles/Vehicle.cs @@ -185,6 +185,8 @@ namespace SanAndreasUnity.Behaviours.Vehicles void OnDisable() { s_vehicles.Remove(this); + + this.OnDisable_Radio(); } void Start() diff --git a/Assets/Scripts/Behaviours/Vehicles/Vehicle_Radio.cs b/Assets/Scripts/Behaviours/Vehicles/Vehicle_Radio.cs index 3461f400..794d5056 100644 --- a/Assets/Scripts/Behaviours/Vehicles/Vehicle_Radio.cs +++ b/Assets/Scripts/Behaviours/Vehicles/Vehicle_Radio.cs @@ -31,6 +31,12 @@ namespace SanAndreasUnity.Behaviours.Vehicles m_currentRadioStationIndex = Random.Range(0, RadioStation.stations.Length); } + void OnDisable_Radio() + { + // need to destroy audio clip here, because otherwise it will stay in memory + this.StopPlayingRadio(); + } + void Start_Radio() { @@ -129,6 +135,8 @@ namespace SanAndreasUnity.Behaviours.Vehicles RadioStation.stations[m_currentRadioStationIndex].currentTime = m_radioAudioSource.time; m_radioAudioSource.Stop(); + + this.DestroyCurrentRadioClip(); } void StartPlayingRadio(bool playImmediately) @@ -175,12 +183,7 @@ namespace SanAndreasUnity.Behaviours.Vehicles m_radioAudioSource.Stop(); // just in case - // destroy current clip - if (m_radioAudioSource.clip != null) - { - Destroy(m_radioAudioSource.clip); - m_radioAudioSource.clip = null; - } + this.DestroyCurrentRadioClip(); var clip = RadioStation.stations[m_currentRadioStationIndex].LoadCurrentClip(); if (clip != null) @@ -194,6 +197,15 @@ namespace SanAndreasUnity.Behaviours.Vehicles } + void DestroyCurrentRadioClip() + { + if (m_radioAudioSource.clip != null) + { + Destroy(m_radioAudioSource.clip); + m_radioAudioSource.clip = null; + } + } + public void SwitchRadioStation(bool next) { int index = m_currentRadioStationIndex;