Scripting / Audio
MusicManager
Singleton (DontDestroyOnLoad) that manages background music with automatic crossfade between scenes. Place one instance per scene with the clip for that scene assigned.
Inspector Variables
| Field | Type | Default | Description |
|---|---|---|---|
musicSource | AudioSource | — | The AudioSource component used for playback. Must be assigned on the prefab. |
musicClip | AudioClip | — | The music clip for this scene's manager to play. |
transitionDuration | float | 1 | Duration in seconds of the volume crossfade between two different tracks. |
Static Properties
| Property | Type | Description |
|---|---|---|
current | MusicManager | The active singleton instance. |
Crossfade Behaviour on Scene Load
| Condition | Result |
|---|---|
No existing MusicManager | New manager becomes singleton, calls DontDestroyOnLoad, and starts playing immediately. |
| Same clip already playing | New manager is destroyed. Music continues uninterrupted. |
| Different clip playing | Crossfade (transitionDuration seconds) starts on the existing manager. New manager is then destroyed. |
DontDestroyOnLoad restriction
Do not place the MusicManager prefab as a child of another
DontDestroyOnLoad object. Unity does not support nested DontDestroyOnLoad hierarchies.