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.
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();
}
}
In the PauseGame() function you are setting the AudioListener.pause to true. So, in the GoToMainMenu() function set the AudioListener.pause to false.
If you pause your game and call PauseGame()
AudioListener.pause
is set to true
.
If you resume you correctly set AudioListener.pause
to false
.
However, if you stop your game (by going into some main menu) and start a new one (or the previous one) the static bool AudioListener.pause
is still false
because of your pause menu intervention.
Even if your starting setup results in pausemenu.Start()
getting called when starting a new game, this will not affect AudioListener.pause
. You have to explicitly call pausemenu.ResumeGame()
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.