# Stopping Audio not working?

**URL:** https://qa.fmod.com/t/stopping-audio-not-working/17657
**Category:** Unity
**Created:** [September 10, 2021, 1:23am UTC](https://qa.fmod.com/t/stopping-audio-not-working/17657 "2021-09-10T01:23:14Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![KrankyPenguin](https://avatars.discourse-cdn.com/v4/letter/k/6de8d8/32.png) [@KrankyPenguin](https://qa.fmod.com/u/KrankyPenguin)
#### Post date: [September 10, 2021, 1:23am UTC](https://qa.fmod.com/t/stopping-audio-not-working/17657/1 "2021-09-10T01:23:14Z")

</div>

Here is my code:

```auto
void StartMusic(int wave)
    {
        bool playSong = false;

        switch (wave)
        {
            case 4:
                songIndex = 0;
                playSong = true;
                break;
            case 9:
                musicInstance.stop(FMOD.Studio.STOP_MODE.ALLOWFADEOUT);
                break;
            case 24:
                songIndex = 1;
                playSong = true;
                break;
        }

        if (playSong)
        {
            musicInstance = RuntimeManager.CreateInstance(music);
            musicInstance.setParameterByName("Song", songIndex);
            musicInstance.start();
        }
    }

```

Just been trying some stuff out with FMOD. The audio event starts playing perfectly fine, but does NOT stop when reaching wave 9. I logged it too so I know that switch is working. Stop just doesn’t work though.

Would really appreciate any help with this! It’s really haulting my progress.

---

<div class="post-metadata">

### Author: ![richard\_simms](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/richard_simms/32/261_2.png) [@richard\_simms](https://qa.fmod.com/u/richard_simms)
#### Post date: [September 10, 2021, 2:31pm UTC](https://qa.fmod.com/t/stopping-audio-not-working/17657/2 "2021-09-10T14:31:41Z")

</div>

From what I can see, the variable `musicInstance` is not scoped into case 9. Try to instantiate it outside the switch (for example, `FMOD.Studio.EventInstance musicInstance;`) so that it can be used in all functions within this piece of code.

---

<div class="post-metadata">

### Author: ![KrankyPenguin](https://avatars.discourse-cdn.com/v4/letter/k/6de8d8/32.png) [@KrankyPenguin](https://qa.fmod.com/u/KrankyPenguin)
#### Post date: [September 10, 2021, 2:50pm UTC](https://qa.fmod.com/t/stopping-audio-not-working/17657/3 "2021-09-10T14:50:22Z")

</div>

oh yeah right above StartMusic() it is instantiated for the whole class. Just didn’t include the whole script.

---

<div class="post-metadata">

### Author: ![KrankyPenguin](https://avatars.discourse-cdn.com/v4/letter/k/6de8d8/32.png) [@KrankyPenguin](https://qa.fmod.com/u/KrankyPenguin)
#### Post date: [September 10, 2021, 3:05pm UTC](https://qa.fmod.com/t/stopping-audio-not-working/17657/4 "2021-09-10T15:05:53Z")

</div>

full script if you are curious:

```auto
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FMODUnity;

public class Music : MonoBehaviour
{
    [EventRef]
    public string music = "";

    FMOD.Studio.EventInstance musicInstance;

    int songIndex = 0;

    private void Start()
    {
        EndlessManager.Instance.onWaveChanged += StartMusic;
    }

    void StartMusic(int wave)
    {
        bool playSong = false;

        switch (wave)
        {
            case 4:
                songIndex = 0;
                playSong = true;
                break;
            case 9:
                StopSong();
                break;
            case 24:
                songIndex = 1;
                playSong = true;
                break;
        }

        if (playSong)
        {
            musicInstance = RuntimeManager.CreateInstance(music);
            musicInstance.setParameterByName("Song", songIndex);
            musicInstance.start();
        }
    }

    private void OnDisable()
    {
        StopSong();
    }

    void StopSong()
    {
        musicInstance.stop(FMOD.Studio.STOP_MODE.ALLOWFADEOUT);
    }
}

```

---

<div class="post-metadata">

### Author: ![richard\_simms](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/richard_simms/32/261_2.png) [@richard\_simms](https://qa.fmod.com/u/richard_simms)
#### Post date: [September 14, 2021, 5:04am UTC](https://qa.fmod.com/t/stopping-audio-not-working/17657/5 "2021-09-14T05:04:47Z")

</div>

Would you be able to show the code for `EndlessManager`? Initial glance of your code seems alright but I’m wondering if multiple event instances are being created on waves 4 and 24.
