Saturday, October 26, 2013

Unity Sound Example - Contiguous File Playback from Event Trigger

This is a quick example showing how the same event trigger can play a different sound asset from an array in order - using a variable, modulo and an AudioClip[] array.

In short:
• Find an in-game object that currently triggers a single sound
• Find the script that is associated with that object that triggers that sound
• Add an AudioClip[] array variable to the top of the code e.g.:
var SoundFileNames : AudioClip[];
• Add a variable to the top of the code for storing the index of the sound file name array e.g.:
var SoundIndex : int = 0;
• Find the audio.Play() function within that script
• Add code above that reads:
SoundIndex = (SoundIndex + 1) % SoundFileNames.length;
audio.clip = SoundFileNames[SoundIndex];
• Remember to inspect the object in Unity via the Inspector and populate the audio clip array with audio assets!