Most Simple C# Script to play a sound from FMOD

Hello, could anyone point me in the direction of the simplest C# code to play a sound in Unity from FMOD, I just can not find this is the FMOD site?
Thanks

FMOD - Scripting Examples | Basic

Essentially all you need is to refer to the path of the file you wish to play. You can do this by directly entering the string, or by using an FMOD Event path in the inspector. Then you can do things such as start, stop, play, play oneshot, etc.

1 Like

Thanks for the help, this is all I found on the FMOD site, but it doesn’t make any sense to me in terms of putting it into Visual Studio as a script?

//--------------------------------------------------------------------
// 4: This shows how to create an instance of an Event and manually
// start it.
//--------------------------------------------------------------------
playerState = FMODUnity.RuntimeManager.CreateInstance(PlayerStateEvent);
playerState.start();

    playerIntro = FMODUnity.RuntimeManager.CreateInstance(PlayerIntroEvent);
    playerIntro.start();

Not sure if you are asking for a way to trigger the audio or just play a sound event.

The Unity integration already have a couple of components that lets you select audio events and play based on conditions.

https://www.fmod.com/resources/documentation-unity?version=2.0&page=game-components.html#studio-event-emitter

Otherwise you can just create simple components for your specific needs.

image

image

1 Like

Many thanks for this, I will research further.

If you’re playing the sounds by yourself you must make sure to call .release after .start() so the audio gets released from memory after it’s done playing.

[EventRef] public string coolAudioReferenceString;
EventInstance coolAudioEventInstance;

coolAudioEventInstance = FMODUnity.RuntimeManager.CreateInstance(coolAudioReferenceString);
coolAudioEventInstance.start();
coolAudioEventInstance.release(); // <---- This here, mucho importante