Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A Basic Audio Modifier for random pitch/volume for an AudioClip #Unity
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BasicAudioModifier : MonoBehaviour {
public List<AudioClip> audioClips = new List<AudioClip>();
public bool mute = false;
public bool ignoreAudioSourceClip = false;
public bool playOnAwake = false;
[Header("Pitch Modifier")]
[Tooltip("0.2f to 1f")]
[Range(0.2f, 1f)]
public float pitchLowRange = .75f;
[Tooltip("0.75f to 2f")]
[Range(0.75f, 2f)]
public float pitchHighRange = 1.25f;
[Header("Volume Modifier")]
[Tooltip("0.2f to 1f")]
[Range(0.2f, 1f)]
public float volumeLowRange = .75f;
[Tooltip("0.8f to 2f")]
[Range(0.8f, 2f)]
public float volumeHighRange = 1f;
private AudioSource audioSource;
void Awake () {
audioSource = GetComponent<AudioSource>();
if(!ignoreAudioSourceClip) {
//delete any null clips, add an if statement if desirable?
audioClips.RemoveAll(clip => clip.Equals(null));
//TODO: when audioSource.playOnAwake is true the event fires before I can override it
if(audioSource.playOnAwake) {
Debug.Log("AudioSource.playOnAwake is true, should be false");
if(playOnAwake) {
void Start () {
public void Play () {
if (!mute) {
audioSource.pitch = Random.Range(pitchLowRange, pitchHighRange);
audioSource.volume = Random.Range(volumeLowRange, volumeHighRange);
int clipId = Random.Range(0, audioClips.Count);
Debug.Log("playing clip: " + audioClips[clipId].name);
audioSource.clip = audioClips[clipId];
Copy link

scawp commented Feb 4, 2022



A simple script to change the pitch/volume of an AudioClip.


Drop the script onto your GameObject containing the AudioSource. Modify the options as desired.

Set PlayOnAwake to false in AudioSource, use PlayOnAwake in the script so the modifiers take effect.

call GetComponent<BasicAudioModifier>().Play(); to replay clip (clip will have a new pitch/volume).

Multiple AudioClips can be added and one will be chosen at random.

if ignoreAudioSourceClip is true then the existing AudioClip in AudioSource will be ignored else it will be added to the random list.

Don't like something here? Fork and modify needed! Let me know if you come up with something cool ;)


Hearing the same noise over and over was driving me insane, this helps. For bigger projects I'd recommend you use something like Fmod or blenda but this is fine for simple needs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment