OK, your help was appreciated and did pinpoint the issue. Here’s how I implemented it.
I moved the AudioSource from the GameObject prefab into a GameObject of its own, parented under the original GameObject.
When I need to destroy a sound gracefully (without popping), I use the script below - ON the AudioSource GameObject under the original (parent) object.
The script simply checks the GameObject that it is attached to see if it is still parented. If it is, nothing is done. If you clear the parenting of the AudioSource GameObject from the parent, the script will fade out and destroy the object the audio script are attached to over numerous frames (dependent on current volume / stepFactor setting) to smooth the audio destruction and alleviate the sharp volume transitions to prevent pop.
Again, thank you, help appreciated - script below to repay the community, if anyone needs it!
JS
– Script Start (javascript) –
#pragma strict
var stepFactor : float = 0.05;
private var aSource : AudioSource;
function Awake()
{
aSource = gameObject.GetComponent(AudioSource);
}
function FixedUpdate()
{
// Once we detach from our parent, we’ll fade and die without audio popping noises
if (transform.parent != null) return;
if (aSource != null) // Be sure this object actually has an audio source!
{
aSource.audio.volume -= stepFactor;
if (aSource.audio.volume > 0) return;
}
else
Debug.Log(“Object named "” + gameObject.name + “" has no AudioSource!”);
Destroy(gameObject);
}
– Script End. Enjoy!
If you use the script and you till hear audio pop, just adjust the stepFactor in the Unity GUI (Inspector view of the audio object the script is attached to). The smaller the step, the more time (and thus FixedUpdate calls) it will take to kill the audio.