- Home /
Reactivate Background Music using "Resume Button" and Escape
Hello everyone,
I have a problem to stop my Pause Menu Music and reactivate the background Music of my Game. The background music stops after using "Escape" and the Pause Menu Music starts to play. But the Pause Menu Music don't stop after using the "Resume" button or "Escape" and the background music also don't start to play.
This is my script to manage the background music:
public GameObject Canvas;
public AudioSource background;
public AudioSource gameOver;
void Start()
{
background.Play();
}
void OnCollisionEnter2D(Collision2D c)
{
if (c.gameObject.tag == "Enemy" || c.gameObject.tag == "Bullet")
{
Canvas.SetActive(true);
Time.timeScale = 0;
background.Stop();
gameOver.Play();
}
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
if (background.isPlaying)
background.Stop();
}
}
and this is my script to manage the PauseMenu and "Resume" Botton:
public GameObject Canvas;
private bool _isShowing;
public AudioSource pauseMusic;
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
PauseGame();
}
}
public void PauseGame()
{
_isShowing = !_isShowing;
Canvas.SetActive(_isShowing);
pauseMusic.Play();
Time.timeScale = _isShowing ? 0 : 1;
}
Answer by IMemeManI · Jan 17, 2018 at 03:44 PM
void Update()
{
if(Input.GetKeyDown(KeyCode.Esc))
{
EnableDisaplePause();
}
}
void EnableDisablePause()
{
if(puaseCanvas.activeInHeirarchy)
{
pauseCanvas.SetActive(false);
Resume();
}
else
{
pauseCanvas.SetActive(true);
Time.timeScale = 0;
mySongSource.Pause();
}
}
void Resume()
{
Time.timeScale = 1;
MySongSource.UnPause();
}
it vague now that I think of it but basically you can use "Pause()" and "UnPause()" on your audio source! That should allow you to resume your song on Resume.
Answer by kyokosagawaotome · Jan 17, 2018 at 04:12 PM
Ok well i fixed it now... my background music starts playing if I use the "Resume" Button and the Pause Music is playing if I use "Escape" ^^ but the new problem is, that may background music won't stop if i use the "escape" button to get into the pause menu. so in the pause menu I have now my Pause Music playing and the background music