Here ya go. Add the script as a component onto your sub-components, populate the list of sound effects manually, and have all your UnityEvents call PlayRandomisedSound() instead of Play().
using UnityEngine;
namespace RPG.Sound
{
public class SoundRandomiser : MonoBehaviour
{
[SerializeField] float lowerPitchRange = 0.8f;
[SerializeField] float upperPitchRange = 1.2f;
[SerializeField] AudioClip[] soundSet = null;
AudioSource audioSource = null;
private void Awake()
{
audioSource = GetComponent<AudioSource>();
}
public void PlayRandomisedSound()
{
// Set the clip to be a random one from the set
audioSource.clip = soundSet[Mathf.RoundToInt(Random.Range(0, soundSet.Length - 1))];
// Set the pitch to a random value within the specified range.
audioSource.pitch = Random.Range(lowerPitchRange, upperPitchRange);
audioSource.Play();
}
}
}
Happy developing!