Search Unity

How to make audio fade out and then stop

Discussion in 'Getting Started' started by KyleL144, Sep 1, 2019.

  1. KyleL144

    KyleL144

    Joined:
    Aug 9, 2019
    Posts:
    21
    Hey,

    I am relevantly new to unity and I have got a main menu with 4 title scenes and the music starts player on the main menu screen and continues onto the 4 title scenes, but I want the music to fadeout and then stop on the fourth title scene. How would i be able to achieve this.

    Down below is my music script to make the music continue:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class Music : MonoBehaviour
    7. {
    8.     // Start is called before the first frame update
    9.  
    10.     public GameObject theManager;
    11.     public string loadLevel;
    12.  
    13.     void Start(){
    14.         Invoke("Update", 5f);
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update(){
    19.         SceneManager.LoadScene(loadLevel);
    20.         DontDestroyOnLoad(theManager);
    21.     }
    22. }
    Any help appreciated and thanks in advance.
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Well for starters, there's no reason to invoke Update, as Update is called automatically once every frame. This is basically doing nothing:
    Code (CSharp):
    1. void Start() {
    2.    Invoke("Update", 5f);
    3. }
    Next, you don't want to load a scene in Update either, since again, Update is called every frame. This is just constantly reloading the same scene:
    Code (CSharp):
    1. void Update() {
    2.    SceneManager.LoadScene(loadLevel);
    3.    DontDestroyOnLoad(theManager);
    4. }
    From the looks of it, you may want to do some scripting tutorials before tackling this.
     
    Anbcdeptrai and Joe-Censored like this.
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I don't see anything about playing audio in your Music class. Looks like a scene manager instead of a music manager.

    To fade out audio you'd lower the volume of the music's AudioSource over time. When you have it faded out enough where you want to stop the music, you just call Stop. In Update or a coroutine would be the typical places you would do this.

    https://docs.unity3d.com/ScriptReference/AudioSource-volume.html
    https://docs.unity3d.com/ScriptReference/AudioSource.Stop.html

    Something along these lines. I just wrote this off the top of my head into the forum, so might be bugs, but you should get the idea.
    Code (csharp):
    1. public AudioSource MusicSource;
    2. private bool musicFadeOutEnabled = false;
    3.  
    4. void Start()
    5. {
    6.     PlayMusic();
    7. }
    8.  
    9. public void PlayerMusic()
    10. {
    11.     musicFadeOutEnabled = false;
    12.     MusicSource.volume = 1f;
    13.     MusicSource.Play();
    14. }
    15.  
    16. public void FadeOutMusic()
    17. {
    18.     musicFadeOutEnabled = true;
    19. }
    20.  
    21. void Update()
    22. {
    23.     if (musicFadeOutEnabled)
    24.     {
    25.         if (MusicSource.volume <= 0.1f)
    26.         {
    27.             MusicSource.Stop();
    28.             musicFadeOutEnabled = false;
    29.         }
    30.         else
    31.         {
    32.             float newVolume = MusicSource.volume - (0.01f * Time.deltaTime);  //change 0.01f to something else to adjust the rate of the volume dropping
    33.             if (newVolume < 0f)
    34.             {
    35.                 newVolume = 0f;
    36.             }
    37.             MusicSource.volume = newVolume;
    38.         }
    39.     }
    40. }
     
    Last edited: Sep 3, 2019
    Vryken likes this.