Audio playback volume inconsistencies

I’m having a problem playing back a clicking sound being used for pressing UI buttons. The problem is that the volume of the playback varies WILDLY from very soft to very loud, I would say in a range from .5x volume to 2x volume. The AudioSource component being used for playback is attached to the AudioListener, so it shouldn’t be a sound field issue.

Here is the playback code. The value for volume being passed is always 1 in this case, and according to the inspector this is the case. I do some pitch variation to break up uniformity, but the volume level flux I am observing does not in any way line up with the pitch variations. The playback function only runs once, and even if it didn’t, I make a call to Stop() before starting. No AudioSource components are building up through time either.

if(!onceSource  (onceSource = (AudioSource)GetComponent(typeof(AudioSource))) == null)
	onceSource = (AudioSource)gameObject.AddComponent("AudioSource");
else if(onceSource.isPlaying)
	onceSource.Stop();
onceSource.clip				  = clip;
onceSource.pitch				 = .9f + UnityEngine.Random.value * .2f;
onceSource.volume				= volume;
onceSource.rolloffFactor	  = rolloffFactor;
onceSource.Play();

Anyone encountered something like this or have any ideas what could be going on? Thanks.

If you cut off the sound as soon as it starts, you’re going to get clicks. Not a click that you get from an audio file - a click that results from cutting the waveform off. Its volume will be seemingly random, but it’s based on where the waveform gets chopped off. Why do you even have that in your code?

Also, have you seen this? It will probably clean your code up.

i wasn’t referring to a noise click, simply that the content of my sound file is that of a button click. Sorry for any confusion there,- the sound plays normally and completely. If you’ll re-read the code, you’ll notice that I don’t stop it AFTER playing, I stop any clip that WAS playing BEFORE playing a new clip. Maybe this is unnecessary, as I’m still unfamiliar with every aspect of Unity, but it certainly isn’t resulting in the problem you described.

To reiterate, the sound file plays in its entirety, but the volume level of the sound playback is completely random. I use this same code to playback all of my sounds, and none of the other sounds seem to be affected in the same way.

If you have this code triggered from a button click, you will inevitably get the problem I described, but only at the moment when you press the button again, if the previous audio file has not completed playback.

Going from what you’ve mentioned, now, I have no clue. If you want to upload a small project file that illustrates the problem, I’ll look into it.