In this section, we are going to look at adding background music to our game levels. We will also be updating our scripts so that our music volume changes at different points of the game.
In the following sections, we are going to do the following:
- Add music to each level.
- When the player completes the level, make the music fade out.
- If the player dies, make the music instantly stop.
- Music will not play on other scenes, only level scenes.
So, let's make a start and add our game music to the level1 scene.
Updating our GameManager prefab
In this section, we are going to update the GameManager game object so that it holds a new game object (called LevelMusic) as a child in the Hierarchy window. We will then assign the LevelMusic's AudioSource component and an MP3 to play. This kind of setup is ideal for a simple game; otherwise, we potentially run...