- Home /
Audio problems with unity after reloading my game from the main menu
I have one problem : when I pause the game , the audio pauses too , but I created a "go back to menu" button , it goes to the main menu but when i hit start again and the game starts running , my audio is dead , i can not hear anything so if anyone knows what should I do please tell me , thanks in advance!
Here is my code : using UnityEngine; using UnityEngine.SceneManagement;
public class pausemenu : MonoBehaviour { public GameObject pauseMenu;
public bool isPaused;
void Start()
{
pauseMenu.SetActive(false);
}
// Update is called once per frame
void Update()
{
if(Input.GetKeyDown(KeyCode.Escape))
{
if(isPaused)
{
ResumeGame();
}
else
{
PauseGame();
}
}
}
public void PauseGame()
{
pauseMenu.SetActive(true);
Time.timeScale = 0f;
isPaused = true;
AudioListener.pause = true;
}
public void ResumeGame()
{
pauseMenu.SetActive(false);
Time.timeScale = 1f;
isPaused = false;
AudioListener.pause = false;
}
public void GoToMainMenu()
{
Time.timeScale = 1f;
SceneManager.LoadScene("menu1");
}
public void QuitTheGame()
{
Application.Quit();
}
}
Unpause the audio listener in Goto$$anonymous$$ain$$anonymous$$enu() function as well. Like this: public void GoTo$$anonymous$$ain$$anonymous$$enu() { Time.timeScale = 1f; AudioListener.pause = false; Scene$$anonymous$$anager.LoadScene("menu1"); }
Answer by skagontale · Sep 25, 2020 at 12:46 PM
the reason that your audio is stopping is because you're setting the timescale to 0 meaning that you are slowing down the speed of the game to none along with the music. a work around this would be to set a public static bool in all of the things that you want to stop and then have an if statement that reads as follows over every line of code that will make an object do something: if (!bool) {enter code that you want the object to be doing} and then in your pause script trigger the bool with: name of script you want to access.name of bool you want to access = true; and then unpause by switching the true with false. as for going to the main menu i would recommend slowing down the music to a stop within a small timeframe and then restarting it when you get to the main menu
Your answer
Follow this Question
Related Questions
The same audio plays differently on a variety of buttons, how can I change this? 0 Answers
Changing two different objects renderer colour 1 Answer
How to get decibel's value of game's sound? 0 Answers
Keeping audio between all but one scene? 1 Answer
How do I only start the audio when player is at max distance 1 Answer