Search Unity

How to fade out and destroy audio on load

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

  1. KyleL144

    KyleL144

    Joined:
    Aug 9, 2019
    Posts:
    21
    Hey, I have got a basic script that alloy the music to continue playing from scene 2 until scene 6, but I have been trying most of the day to get it where the audio starts to fade out and the is destroyed on scene 6, before loading into scene 7.

    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 AudioSource music;
    12.     public string loadLevel;
    13.     public float fadeOutTime = 3f;
    14.     bool fading;
    15.     float fadePerSec;
    16.  
    17.     void Awake()
    18.     {
    19.  
    20.         DontDestroyOnLoad(theManager);
    21.         SceneManager.sceneLoaded += OnSceneLoaded;
    22.     }
    23.  
    24.     void OnDestroy()
    25.     {
    26.         SceneManager.sceneLoaded -= OnSceneLoaded;
    27.     }
    28.  
    29.     void Update()
    30.     {
    31.         if (fading)
    32.         {
    33.             music.volume = Mathf.MoveTowards(
    34.             music.volume, 0, fadePerSec * Time.deltaTime);
    35.         }
    36.     }
    37.     void OnSceneLoaded(Scene scene, LoadSceneMode mode)
    38.     {
    39.         if ((scene.buildIndex != 2) && (scene.buildIndex != 3) && (scene.buildIndex != 4) && (scene.buildIndex != 5) && (scene.buildIndex != 6))
    40.         {
    41.             fading = true;
    42.             fadePerSec = music.volume / fadeOutTime;
    43.             Destroy(theManager, fadeOutTime);
    44.         }
    45.  
    46.     }
    47. }
    Here are 2 screenshots of the layout of the project like audio source ect:

    project.png scriptmanager.png

    Any help appreciated
     
    Last edited: Sep 6, 2019
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
  3. KyleL144

    KyleL144

    Joined:
    Aug 9, 2019
    Posts:
    21
    The actual music clip is placed in an audio source and the above script is the only script that i created for the music as I followed in of a tutorial and the above script makes the music continue through scene 2 until scene 5 but on scene 6 then i have been trying to get the music to fade out and stop before loading scene 7 :/
     
    Last edited: Sep 5, 2019
  4. KyleL144

    KyleL144

    Joined:
    Aug 9, 2019
    Posts:
    21
    I have updated the above with my new code, but the music still wont fade out and destroy on scene 6
     
  5. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    How are you debugging, I don't see any Debug.Log statements.

    if ((scene.buildIndex != 2) && (scene.buildIndex != 3) && (scene.buildIndex != 4) && (scene.buildIndex != 5) && (scene.buildIndex != 6))

    Why not just say "if buildindex == 1"?
     
    Joe-Censored likes this.