How to detect state of Toggle button?

Say I have a Toggle button like a checkbox with a Boolean ON/OFF state.

I want to have the button play an FMOD event for “ON” and a different event for “OFF”.

Is there a simple way with FMOD-Unity integration to recognize this logic and play a separate event? FMOD Studio Event Emitter script only has one slot.

I know I can “cheat” it by having a single FMOD event that alternates between two sounds, but that isn’t as reliable.

Hello

You could use two different emitter objects and just start and pause them as the toggle changes.

if (toggle == true)
{
    // Stop the other audio source 
    if (emitter2.IsPlaying())
        emitter2.Stop();

    if (emitter1 != null && audio1.IsPlaying() == false)
        emitter1.Play();
}
else
{
    if (emitter1.IsPlaying())
        emitter1.Stop();

    if (emitter2 != null && audio2.IsPlaying() == false)
        emitter2.Play(); 
}

How do you mean having an event with two sounds isn’t reliable? Using a parameter to change audio sources in a single event would be a safe a quick way to change audio sources.

if (toggle == true)
{
    if (emitter.EventInstance.isValid())
        emitter.EventInstance.setParameterByName("ParameterName", 0);
}
else
{
    if (emitter.EventInstance.isValid())
        emitter.EventInstance.setParameterByName("ParameterName", 1); 
}

Hope this helps!

Thanks @Connor_FMOD !

Oh I’m referring to a sort of different scenario without scripted logic like yours, where it doesn’t actually know what state the Toggle is in, it’s just alternating between two sounds in a single event with a multi-instrument, so there are things that can happen that trip it up and make it play the wrong “ON” or “OFF” sound.

I’m a bit confused,

What is ‘it’ in this situation? Would this be the emitter or the event?

Are the sounds playing all the time or is this a single trigger?

Could I see an example event? Or an example where this would come up in a game?

Ah! Nevermind about that past example, it was a non-Unity one from long ago and not applicable here. Just something that came to mind. Your recent “two different emitter objects” sounds like it should sort this out, again, thank you. :slight_smile:

1 Like