# How to detect state of Toggle button?

**URL:** <https://qa.fmod.com/t/how-to-detect-state-of-toggle-button/19396>\
**Category:** Unity\
**Created:** [October 12, 2022, 10:19pm UTC](https://qa.fmod.com/t/how-to-detect-state-of-toggle-button/19396 "2022-10-12T22:19:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![torley-gallium](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/torley-gallium/32/3688_2.png) [@torley-gallium](https://qa.fmod.com/u/torley-gallium)\
**Post date:** [October 12, 2022, 10:19pm UTC](https://qa.fmod.com/t/how-to-detect-state-of-toggle-button/19396/1 "2022-10-12T22:19:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Connor\_FMOD](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/connor_fmod/32/3685_2.png) [@Connor\_FMOD](https://qa.fmod.com/u/Connor_FMOD)\
**Post date:** [October 13, 2022, 2:26am UTC](https://qa.fmod.com/t/how-to-detect-state-of-toggle-button/19396/2 "2022-10-13T02:26:45Z")

</div>

Hello

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

```auto
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.

```auto
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!

---

<div class="post-metadata">

**Author:** ![torley-gallium](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/torley-gallium/32/3688_2.png) [@torley-gallium](https://qa.fmod.com/u/torley-gallium)\
**Post date:** [October 13, 2022, 3:44am UTC](https://qa.fmod.com/t/how-to-detect-state-of-toggle-button/19396/3 "2022-10-13T03:44:45Z")

</div>

Thanks @Connor_FMOD !

> [@Connor\_FMOD](#):
>
> How do you mean having an event with two sounds isn’t reliable?

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.

---

<div class="post-metadata">

**Author:** ![Connor\_FMOD](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/connor_fmod/32/3685_2.png) [@Connor\_FMOD](https://qa.fmod.com/u/Connor_FMOD)\
**Post date:** [October 14, 2022, 12:53am UTC](https://qa.fmod.com/t/how-to-detect-state-of-toggle-button/19396/4 "2022-10-14T00:53:44Z")

</div>

I’m a bit confused,

> [@torley-gallium](#):
>
> where it doesn’t actually know what state the Toggle is in

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

> [@torley-gallium](#):
>
> it’s just alternating between two sounds in a single event with a multi-instrument

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?

---

<div class="post-metadata">

**Author:** ![torley-gallium](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/torley-gallium/32/3688_2.png) [@torley-gallium](https://qa.fmod.com/u/torley-gallium)\
**Post date:** [October 14, 2022, 9:38am UTC](https://qa.fmod.com/t/how-to-detect-state-of-toggle-button/19396/5 "2022-10-14T09:38:01Z")

</div>

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. 🙂
