Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Audio How do I make the background-music stop playing when the pause-menu is active?

Discussion in 'Audio & Video' started by mr_pineapple_44, Sep 23, 2023.

  1. mr_pineapple_44

    mr_pineapple_44

    Joined:
    Aug 18, 2023
    Posts:
    3
    This is my code:

    PauseMenuScript

    using System.Collections;
    using System.Collections.Generic;
    using TMPro;
    using UnityEngine;
    using UnityEngine.UI;
    using UnityEngine.SceneManagement;

    public class PauseMenuScript : MonoBehaviour
    {
    [SerializeField] GameObject pauseMenu;
    public TextMeshProUGUI titleText;
    public Button resumeButton;

    public bool pauseBool = false;

    // Start is called before the first frame update
    void Start()
    {
    gameObject.SetActive(pauseBool);
    }

    // Update is called once per frame
    void Update()
    {

    }


    public void ActivePauseMenu()
    {
    pauseMenu.SetActive(true);
    }

    public void ClosePauseMenu()
    {
    pauseMenu.SetActive(false);
    }


    }

    PauseHandlerScript

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class PauseHandlerScript : MonoBehaviour
    {

    public bool paused = false;

    public GameObject pauseMenu;

    void Start()
    {
    pauseMenu.GetComponent<PauseMenuScript>().ActivePauseMenu();
    }


    void Update()
    {

    if (Input.GetKeyDown(KeyCode.Tab))
    {
    paused = !paused;
    }
    if (paused)
    {
    Time.timeScale = 0;
    pauseMenu.SetActive(paused);


    }
    else
    {
    Time.timeScale = 1;
    pauseMenu.SetActive(paused);
    }



    }
    }

    Here is my pausemenu and audio source component.

    upload_2023-9-23_16-52-4.png
     
  2. SeventhString

    SeventhString

    Unity Technologies

    Joined:
    Jan 12, 2023
    Posts:
    290
    You could use an AudioMixer Snapshot to handle that gracefully, there is a good tutorial on the matter here.

    Basically, you'll be able to save a mixer state for multiple scenarios (such as "Paused" and "Unpaused") and then switch between them.