FMOD Event Instances
Event Instances in FMOD + Unity: A Hands-On Guide⌗
What You’ll Build⌗
By the end, you will:
- Create an FMOD Event for background music
- Assign it to a Bank and connect it to Unity
- Instantiate, start, stop (with proper Stop Modes), and release an EventInstance in C#
- Verify correct lifecycle behavior using FMOD Profiler
Setup⌗
Open FMOD Studio with the 3D Game Kit Project and the Unity 3D Game Kit Project.
Note: I had to update FMOD Studio and FMOD for Unity to the latest version (2.03.09 and 2.03.09 respectively) to get this working on my machine. See the FMOD Download Page.
Events⌗
Events are how your game plays audio—sound effects, music, and dialogue. They’re flexible: you can build them with multiple tracks and layer or swap different assets. Think of an event as the unit you reference from code. In C#, you trigger an event (by creating and starting an EventInstance) whenever you want a sound to play.
1) Create a Music Event in FMOD⌗
- In the Events Browser, right-click and choose
Event Preset → 2D Timeline. Name the eventGameplay_Musicand place it inside a folder calledMusic. This event will serve as your background music for the first level. - Open the Audio Bin (
Ctrl+3on Windows /Cmd+3on macOS`). Drag your chosen music file into this window to import it.- You can also import audio via
File → Import Assets.
- You can also import audio via
- With the
Gameplay_Musicevent selected, drag your audio file from the Audio Bin onto a track in the event’s timeline.- Test playback using the transport controls at the top or by pressing the spacebar.
- To loop the music, right-click the audio clip, choose
Add Loop Region, and extend the loop region to cover the full track.
- Assign the event to a Bank, so Unity can load it at runtime.
- Right-click the event and select
Assign to Bank → Master Bank. - Banks package your FMOD events and assets into loadable groups for the game.
- Right-click the event and select
- Build your Banks (
File → Buildor pressF7).- Building compiles and exports your latest FMOD data so Unity can recognize your new event.
Unity Setup⌗
- Add the
FMOD Studio Bank Loaderscript to your scene if it’s not already there. This object handles loading your FMOD Banks at runtime. LoadMaster BankandMaster Bank.stringson start. - Add the
FMOD Studio Listenerscript to your main camera (CameraBrain). This object represents the player’s listening position for 3D audio. - Ensure that Unity is pointing to the FMOD project you’re currently using.
- Go to
FMOD → Edit Settingsand set theSource Project Pathto your FMOD project folder. - Set the
Build PathtoBuild/Desktopfolder.
- Go to
2) Bring the Event into Unity⌗
Open the Level 1 scene in Unity Project tab:
3DGameKit -> Scenes -> GamePlay.
- In Unity, create an empty GameObject named
Music Object. - In your Assets, create a folder in
3DGameKit -> ScriptscalledFMOD Scripts. - Create a new
MonoBehaviourscript calledMusicPlayer.csand attach it toMusic Object. - Open the script in your code editor.
3) Declare the EventInstance⌗
Create a field to hold the FMOD event instance:
using UnityEngine;
public class MusicPlayer : MonoBehaviour
{
// Treat this as a single FMOD data type for now.
private FMOD.Studio.EventInstance _musicInst;
}
Concept: An EventInstance is a runtime “copy” of your FMOD Event. You can create multiple instances for overlapping or independent playback.
4) Choose How You Reference the Event Path⌗
You can find your event path in FMOD Studio’s Events Browser. Right-click the event and select Copy Path. It will look something like event:/Music/Gameplay_Music.
You have two reliable approaches:
A) Hard-code the Event Path⌗
void Start()
{
_musicInst = FMODUnity.RuntimeManager.CreateInstance("event:/Music/Gameplay_Music");
}
B) Use a searchable inspector field (recommended for reuse)⌗
public FMODUnity.EventReference eventPath;
void Start()
{
_musicInst = FMODUnity.RuntimeManager.CreateInstance(eventPath);
}
- With
FMODUnity.EventReference, Unity’s Inspector exposes a browser to pick the exact FMOD Event. - Great for reusing the same script across different objects that trigger different events.
5) Start the Event on Scene Start⌗
void Start()
{
_musicInst.start();
// Optional: immediately schedule the instance to be released when it’s done
_musicInst.release();
}
Note on .start(): Calling .start() on a currently-playing instance restarts it from the beginning. Plan for this if you retrigger.
Play your game and you should hear your music start when the scene loads!
6) Stop Gracefully on Scene Exit (or Object Destruction)⌗
Use OnDestroy() to ensure your music stops when the level ends or the object is removed:
void OnDestroy()
{
// Choose an appropriate Stop Mode:
_musicInst.stop(FMOD.Studio.STOP_MODE.ALLOWFADEOUT);
// If you did NOT call release() in Start(), you can release here:
// _musicInst.release();
}
Choosing a Stop Mode
STOP_MODE.IMMEDIATE– hard stop (clicks/cutoffs if you haven’t designed for it)STOP_MODE.ALLOWFADEOUT– respects envelopes/modulation (preferred for music) Pair this with an AHDSR modulator on the Event’s Master volume in FMOD for musical fades.
7) Manage Instance Lifetime with .release()⌗
.release()frees the native resources for an instance.- If called while the event is playing, it defers destruction until playback stops (or is stopped).
- Strategy:
- Call
release()right afterstart()for fire-and-forget music cues. - Or defer
release()toOnDestroy()if you need to keep explicit control of the instance’s lifecycle.
- Call
8) Verify with FMOD Profiler (Live Update)⌗
- Enter Play Mode in Unity.
- In FMOD Studio, open Window → Profiler.
- Click Live Update, connect to
localhost(if FMOD and Unity run on the same machine). - Press Record to see sessions and real-time metrics.
- Play through your level; when the scene unloads or the object is destroyed, confirm:
- Instances count drops to zero (if released)
- The master bus signal reflects your fades and stops as expected
This confirms that your Start/Stop/Release logic works under actual gameplay conditions.
Full Example Script⌗
using UnityEngine;
public class MusicPlayer : MonoBehaviour
{
// Pick the FMOD Event via the Inspector
public FMODUnity.EventReference eventPath;
private FMOD.Studio.EventInstance _musicInst;
void Start()
{
// Create and start the instance when the scene starts
_musicInst = FMODUnity.RuntimeManager.CreateInstance(eventPath);
_musicInst.start();
// Fire-and-forget: schedule cleanup when playback stops
_musicInst.release();
}
void OnDestroy()
{
// Stop gracefully on scene/object teardown
_musicInst.stop(FMOD.Studio.STOP_MODE.ALLOWFADEOUT);
// If you didn’t call release() in Start(), release here instead:
// _musicInst.release();
}
}
Troubleshooting Checklist⌗
- Built Banks after changes in FMOD? If not, Unity won’t have the latest events.
- Event path correct? Use the FMOD Event Browser in Unity (via an
EventReferencefield) to avoid typos. - Hearing abrupt cuts? Switch to
ALLOWFADEOUTand add an AHDSR envelope on the Event’s Master volume. - Instances not freeing? Ensure
.release()is called either afterstart()or inOnDestroy()and verify in Profiler.