Game Development Reference
In-Depth Information
audio.volume = bgVolume;
ranMax = SongList.Count;
}
void Update()
{
if(playRandomly)
PlayRandom();
else
Playlist();
}
In the
Start
function, we assign the volume of
AudioSource
to our
volume
vari-
able. Next, we set the maximum value to the variable for our random function to the
amount of songs that we have in our list. In the
Update
function, we call the func-
tions that actually play the music. To do this, we check the
bool
variable that we
created earlier to decide which system to use.
An added feature we can place in our script will allow us to play a single song re-
peatedly. Add this function to the script:
void PlayRepeat(AudioClip Song)
{
audio.clip = Song;
audio.loop = true;
audio.Play();
}
This function will take the
AudioClip
value, which will be the song to be played.
Next, we assign the song to
AudioSource
, set its looping property to
true
, and
finally, play it.
The last feature that we will add to our script will allow us to change the speed of the
song by using the
pitch
property. Add this function to the script: