Question by
Ejat · May 17, 2020 at 09:33 AM ·
pauseresumebackground music
How to stop Background music from resetting when i resume game
Hi everyone, i want my background music to resume when i resume the game. However, the unpause function is not working for me as my background music resets when i resume the game.
Pause
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class Pause : MonoBehaviour
{
public GameObject PauseMenu, BackgroundMusic;
private int currentSceneIndex;
public void PauseButton()
{
BackgroundMusic.GetComponent<AudioSource>().Pause();
Time.timeScale = 0;
currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
PlayerPrefs.SetInt("SavedScene", currentSceneIndex);
PauseMenu.gameObject.SetActive(true);
}
}
Unpause
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PauseMenu : MonoBehaviour
{
public GameObject BackgroundMusic;
private int sceneToContinue;
public void MainMenu ()
{
SceneManager.LoadScene(0);
PlayerPrefs.DeleteKey("Score");
}
public void Resume()
{
sceneToContinue = PlayerPrefs.GetInt("SavedScene");
if (sceneToContinue != 0)
{
BackgroundMusic.GetComponent<AudioSource>().UnPause();
SceneManager.LoadScene(sceneToContinue);
Time.timeScale = 1;
}
else
return;
}
}
Would appreciate it if anyone can identify any errors in my code! :)
Comment
Your answer
Follow this Question
Related Questions
how to Pause game if score exceeds a number and display text and button to resume. 2 Answers
When Resuming game after losing focus I just get a black screen? 0 Answers
Best way to Pause/Resume an object rotation script? 0 Answers
Pause scene when character hit object, then resume it from another scene 0 Answers