Wednesday, October 23, 2013

Unity Sound Example - Random Audio File Playback

A quick demo that makes use of AudioClip[] array, audio.clip and Random.Range() to randomise the audio asset file that is played back when an event is triggered.

In short, the process is:
• Find an in-game object that currently triggers a single sound
• Find the script that is associated with that object that triggers that sound
• Define a new variable at the start of that script to create an array of audio clips,  e.g. var RandomSounds: AudioClip[];
• Find a reference to audio.Play(); within the script
• Randomise the selection of the audio clip in the line before audio.Play(); e.g. audio.clip = RandomSounds[Random.Range(0,RandomSounds.length)];
• Save the script and run the game again