Create a randomized playlist with the Audio Random Container
Create and use a randomized audio playlist.
Read time 2 minutesLast updated 13 days ago
To create a randomized playlist, you must create an Audio Random Container and then play it in the game.
You can use a randomized playlist to randomize and play almost any sound, such as footsteps, impacts, and AI dialog.
Prerequisites
You can create and use a randomized playlist if you know how to do the following:
- Create a GameObject
- Add a button to the scene
Create an Audio Random Container
You can create an Audio Random Container from more than one place.
From the menu
In the Unity Editor, do the following:
- Select Assets > Create > Audio > Audio Random Container.
An empty Audio Random Container asset appears in the Project window under Assets.
From the Project view
In the Unity Editor, do the following:
- Right click in the Project window to bring up the asset menu that contains a working menu item.
- Select Create.
- Select Audio.
- Select Audio Random Container.
An empty Audio Random Container asset appears in the Project window under Assets.
From the window
In the Unity Editor, do the following:
- Select Window.
- Select Audio.
- Select Audio Random Container.
An empty Audio Random Container asset appears in the Project window under Assets.
Play an Audio Random Container in the game
To play an Audio Random Container, you must integrate it with the Audio Source component and then play it, such as with one of the game trigger options.
Integrate with the Audio Source component
You can only use the Audio Random Container in conjunction with the Audio Source component. It's the emitter for the Audio Random Container and determines whether the Audio Random Container has a position in the world or sounds like it doesn't have a position. For more information, refer to Audio Source.
To integrate, do the following:
- Create an Audio Random Container from the menu, the Project view, or the window.
- Create a GameObject.
- In the Inspector window, select Add Component.
- Select Audio > .Audio Source
?
- Next to AudioResource, choose the Audio Random Container you created.
You have integrated the Audio Random Container with the Audio Source component. You can play it with the PlayOnAwake bool, the API, or the different game trigger options.
Use game triggers
Sounds in games are often triggered by a game event or by a trigger. You can use game triggers, such as buttons, to play the Audio Random Container:
- Add a button to a scene.
- In the Inspector window, select + to add an On Click event.
- Select the Audio Random Container.