Search Unity

Question How to fade-out audio

Discussion in 'Audio & Video' started by KyleL1445566, Jul 30, 2020.

  1. KyleL1445566

    KyleL1445566

    Joined:
    Jul 28, 2020
    Posts:
    2
    Hey,

    I am fairly new to unity, but I have go 3 basic scenes:

    Scene 1
    Scene 2
    And Main Menu

    The audio continues from scene 1 onto scene 2, but I want the audio to fade before loading the main menu.

    I have tried to find audio, but their is no solution for this

    Music Continuance script below:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class StartupMusic : MonoBehaviour
    6. {
    7.     static bool AudioBegin = false;
    8.     void Awake()
    9.     {
    10.         if (!AudioBegin)
    11.         {
    12.             GetComponent<AudioSource>().Play();
    13.             DontDestroyOnLoad(gameObject);
    14.             AudioBegin = true;
    15.         }
    16.     }
    17.     void Update()
    18.     {
    19.         if (Application.loadedLevelName == "main_menu")
    20.         {
    21.             GetComponent<AudioSource>().Stop();
    22.             AudioBegin = false;
    23.         }
    24.     }
    25. }
    Here is the scene layout

    layout.png

    Any help appreciated and thanks in advance