Stopping Audio not working?

Here is my code:

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.

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.

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

full script if you are curious:

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);
    }
}

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.