I want to fade-out music in update method ... fade-out method is works but its speed is so high and fade out effect does not feel. Kindly help me.

void Update()
{

    if (GameController.sharedInstance().isGamePause)
    {
        audioSource.Pause();
    }
    if (GameController.sharedInstance().isGamePause == false )
    {
        if (audioSource.isPlaying == false)
            audioSource.Play();
    }
   
    if (time.getCurrentTime() == 83 && state != STATE_CLIP2 )
    {
        state = STATE_CLIP2;
    }

    if(time.getCurrentTime() == 82 && isFadeOut)
    {
        audioSource.clip = clip2;
        audioSource.volume = intialVolume;
        audioSource.Play();
    }

    if (state == STATE_CLIP2 && (time.getCurrentTime() > 83 || GameController.sharedInstance().isPowerUpSelected))
    {
        audioSource.Pause();
        audioSource.volume = intialVolume;
        audioSource.clip = clip1;
        
        audioSource.Play();
        state = STATE_CLIP1;
        GameController.sharedInstance().isPowerUpSelected = false;
        isFadeOut = false;
    }

    if (state == STATE_CLIP1 && (time.getCurrentTime() < 83 || GameController.sharedInstance().isPowerUpSelected))
    {
       
        fadeOutMusic(audioSource);
        isFadeOut = true;
        
        audioSource.Pause();
        audioSource.volume = intialVolume;
        audioSource.clip = clip2;
       
        audioSource.Play();
        state = STATE_CLIP2;
        GameController.sharedInstance().isPowerUpSelected = false;
        isFadeOut = false;
    }

    if(time.getCurrentTime() == 82 && !isFadeOut)
    {
        fadeOutMusic(audioSource);
        isFadeOut = true;
    }
}

private void fadeOutMusic(AudioSource audioSource)
{
    while (audioVolume >= 0.1)
    {
        audioVolume -= 0.1f * Time.deltaTime;
        audioSource.volume = audioVolume;
    }
}

in Update you should run the coroutine, that will continue executing after Update finished.

like this:

   private IEnumerator fadeOutMusic(AudioSource audioSource)
   {
     while (audioSource.volume >= 0.1){
         audioSource.volume -= 0.1f * Time.deltaTime;
         yield return;
     }
   }

and when you want to fadeout sound use this:

   StartCoroutine(fadeOutMusic(yourAudioSourceHere))

Or you could avoid using coroutines, just fire a bool flag, and decrease a volume a little every Update if it’s fired (but NOT from max to 0 in one Update call!). And don’t forget to set flag to false when volume will reach 0f :slight_smile: