Could not stop the sound instances when transiting from another scene

Dear,

I’m having trouble stopping the sound instances using OnDestroy() when transitioning from the level scene to the main scene. Interestingly, it works fine when transitioning from the main scene to the level scene. Although the OnDestroy() function is executed, the stop and release commands don’t seem to work for some unknown reason. Could someone please take a look at my code and provide some guidance?

void Update(){  
    PlayAirWind();
    PlayVehicleSound();
}

void OnDestroy(){  // transit to other scense need to remove the instance that created on Start()
    wind.stop(FMOD.Studio.STOP_MODE.ALLOWFADEOUT);
    wind.release();
    vehicle.stop(FMOD.Studio.STOP_MODE.ALLOWFADEOUT);
    vehicle.release();
    // enemyZone.stop(FMOD.Studio.STOP_MODE.ALLOWFADEOUT);
    // enemyZone.release();
}

void PlayAirWind(){
    FMOD.Studio.PLAYBACK_STATE fallState;
    wind.getPlaybackState(out fallState);
    if (!playerController.isOnGround){
        wind.setParameterByName("Height", playerController.relativeHeight);
        if (fallState != FMOD.Studio.PLAYBACK_STATE.PLAYING){
            wind.start();
        }
    }
    if (playerController.isOnGround || playerController.moveSpeed == 0){
        wind.stop(FMOD.Studio.STOP_MODE.ALLOWFADEOUT);
    }
}

void PlayVehicleSound(){
    FMOD.Studio.PLAYBACK_STATE vehicleState;
    vehicle.getPlaybackState(out vehicleState);
    if (playerController.vehicleOn){
        vehicle.setParameterByName("Height", 20);
        if (vehicleState != FMOD.Studio.PLAYBACK_STATE.PLAYING){
            vehicle.start();
        }
    } else {
        vehicle.stop(FMOD.Studio.STOP_MODE.ALLOWFADEOUT);
    }
}

I do not know where I did wrong. Thanks.

Hi,

I would suggest logging the result of the stop() and release() functions, most FMOD functions return and FMOD.RESULT (FMOD Engine | Core API Reference - Common) which can aid in debugging why functions aren’t behaving as expected:

FMOD.RESULT result = wind.stop(FMOD.Studio.STOP_MODE.ALLOWFADEOUT);
if (result != FMOD.RESULT.OK)
{
    Debug.Log(result);
}

Could you please also change your FMOD Logging Level to Warning
image
This will log any other errors that may be occurring.

Hope this helps!

1 Like

Thanks, it really helps to force me find the bug at another script which I did not pay attention as before. The above code actually works properly.

1 Like