# Prevent sound event to be played if it is already playing

**URL:** <https://qa.fmod.com/t/prevent-sound-event-to-be-played-if-it-is-already-playing/19585>\
**Category:** FMOD Studio\
**Tags:** unity\
**Created:** [November 27, 2022, 12:51am UTC](https://qa.fmod.com/t/prevent-sound-event-to-be-played-if-it-is-already-playing/19585 "2022-11-27T00:51:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![BrokenFuse](https://avatars.discourse-cdn.com/v4/letter/b/4af34b/32.png) [@BrokenFuse](https://qa.fmod.com/u/BrokenFuse)\
**Post date:** [November 27, 2022, 12:51am UTC](https://qa.fmod.com/t/prevent-sound-event-to-be-played-if-it-is-already-playing/19585/1 "2022-11-27T00:51:27Z")

</div>

In Unity:

Scene A:  
A loop sound event “music” is playing at start up (Event Emitter - Object Start).  
Scene B:  
The same loop sound event “music” is playing at start up (Event Emitter - Object Start).

If I switch from Scene A to Scene B:  
I want to prevent the “music” sound event in Scene B to be played as it is already playing from Scene A.  
But, if I switch from Scene C (no such sound) to Scene B:  
I want to play the “music” sound event.

Any thoughts ?

Attempt:  
I tried to detect if the the music instance is playing (c.f. below).  
The method is working, but only while staying in Scene A.  
The moment I switch to Scene B, I no longer get the state of the instance.

> public class AudioInstance : MonoBehaviour  
> {
> 
> [SerializeField]  
> private FMODUnity.StudioEventEmitter emitter;
> 
> public static bool IsPlaying(FMOD.Studio.EventInstance instance)  
> {  
> FMOD.Studio.PLAYBACK\_STATE state;  
> instance.getPlaybackState(out state);  
> return state != FMOD.Studio.PLAYBACK\_STATE.STOPPED;  
> }
> 
> ```
> void Update()
> {
> if (emitter.IsPlaying())
> {
> Debug.Log("Emitter is playing");
> }
> else if (!emitter.IsPlaying())
> {
> Debug.Log("Emitter is not playing");
> }
> }
> 
> ```
> 
> }

---

<div class="post-metadata">

**Author:** ![tweexmusic](https://avatars.discourse-cdn.com/v4/letter/t/977dab/32.png) [@tweexmusic](https://qa.fmod.com/u/tweexmusic)\
**Post date:** [November 28, 2022, 8:06pm UTC](https://qa.fmod.com/t/prevent-sound-event-to-be-played-if-it-is-already-playing/19585/2 "2022-11-28T20:06:40Z")

</div>

It sounds like the issue is that your gameObject holds your event emitter component that exists in Scene A is destroyed when the scene ends at you transition.

When I create music managers to play across different scenes, I use the DontDestroyOnLoad() function inside the the Awake callback like this:

```auto
private void Awake()
    {
        DontDestroyOnLoad(this);
    }

```

That way, the gameObject instance that I’m referencing that controls the music is not released from memory when I change scenes and I can continue using that same instance.

---

<div class="post-metadata">

**Author:** ![mathew](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/mathew/32/431_2.png) [@mathew](https://qa.fmod.com/u/mathew)\
**Post date:** [November 28, 2022, 10:12pm UTC](https://qa.fmod.com/t/prevent-sound-event-to-be-played-if-it-is-already-playing/19585/3 "2022-11-28T22:12:13Z")

</div>

Another approach might be to set the max instances of the Event in Studio to one, with the stealing mode to none. That way if the Event isn’t playing it will start, if it is playing the new instance won’t start.

---

<div class="post-metadata">

**Author:** ![BrokenFuse](https://avatars.discourse-cdn.com/v4/letter/b/4af34b/32.png) [@BrokenFuse](https://qa.fmod.com/u/BrokenFuse)\
**Post date:** [November 28, 2022, 10:37pm UTC](https://qa.fmod.com/t/prevent-sound-event-to-be-played-if-it-is-already-playing/19585/4 "2022-11-28T22:37:32Z")

</div>

Thank you both for your answers.  
Max instance set to 1 and stealing set to none worked right away.
