José Guallpa Spring 25'
Inspired by: Luvseatshawty - "How to Add a Soundtrack to Your Unity Game" tutorial
This tutorial walks through how to add a multi-track soundtrack to your Unity project that:
Randomly shuffles tracks.
Plays continuously between scenes.
Ensures no track repeats until all others are played.
Includes volume control and basic inspector visibility toggles.
This approach is ideal for games or immersive experiences (like my MR Spotify map project) where you want music to enhance the environment and persist across levels.
Import Songs: Convert your tracks to .mp3 format. Place them in a folder inside your Assets directory, e.g., Assets/Soundtrack.
Create a Music Manager Object:
In the Unity hierarchy, create an empty GameObject called MusicManager.
Add an AudioSource component.
Uncheck "Play on Awake".
Create and attach a new script called MusicManager.cs.
Inside MusicManager.cs:
public class MusicManager : MonoBehaviour
{
private AudioSource _audioSource;
public AudioClip[] songs;
public float volume = 0.5f;
private float _trackTimer;
private bool[] _beenPlayed;
private int _songsPlayed;
void Awake()
{
DontDestroyOnLoad(gameObject);
}
void Start()
{
_audioSource = GetComponent<AudioSource>();
_beenPlayed = new bool[songs.Length];
ChangeSong(Random.Range(0, songs.Length));
}
void Update()
{
_audioSource.volume = volume;
if (_audioSource.isPlaying)
_trackTimer += Time.deltaTime;
if (!_audioSource.isPlaying || _trackTimer >= _audioSource.clip.length)
TryChangeSong();
}
void TryChangeSong()
{
if (_songsPlayed >= songs.Length)
{
_songsPlayed = 0;
for (int i = 0; i < _beenPlayed.Length; i++)
_beenPlayed[i] = false;
}
int songIndex = Random.Range(0, songs.Length);
int attempts = 0;
while (_beenPlayed[songIndex] && attempts < 100)
{
songIndex = Random.Range(0, songs.Length);
attempts++;
}
if (!_beenPlayed[songIndex])
{
ChangeSong(songIndex);
}
}
void ChangeSong(int index)
{
_audioSource.clip = songs[index];
_audioSource.Play();
_trackTimer = 0;
_beenPlayed[index] = true;
_songsPlayed++;
}
}
By placing DontDestroyOnLoad(gameObject); in Awake(), the soundtrack continues even when switching scenes.
You can use buttons or sliders in the UI to expose the volume field and let players adjust it.
Use Debug.Log() to help test which songs are being played.
Add a skip track button that manually calls TryChangeSong().
In my project, I used this soundtrack setup to loop trending music from Mexico City. Users interact with the Mexican flag in a passthrough MR environment and this script handles audio playback in the background. Works well for immersive audio experiences with cultural learning goals.
Big shoutout to Luvseatshawty for making a funny, in-depth, and actually helpful tutorial. Highly recommend watching his full YouTube video for the walkthrough. https://www.youtube.com/watch?v=_FlgWmTITcI.
Future Add-ons
Add Spotify API integration to rotate songs weekly.
Create UI to display current track and let users skip.
Log which songs users interact with for later analysis.