Can a kind soul explain how to pause one audio.clip to play another audio.clip on activation of the pause menu please?
I tried to switch songs on button press but no cigar. Audio kept going crazy repeating itself in stutters and playing over other audio tracks. Totally stumped on this one. Tried so many different tutorials and scripting experiments. Just no luck.
Only 2 scripts are involved in this issue. One script for audio manager and one script for pause menu. Both of these script are posted below.
ChangeMusic.cs - Audio Manager Script
//ChangeMusic.cs
using UnityEngine;
using System.Collections;
public class ChangeMusic : MonoBehaviour
{
public AudioClip level2Music;
public AudioClip level1Music;
public AudioSource source;
public bool paused = false;
void Awake ()
{
source = GetComponent<AudioSource>();
}
void OnLevelWasLoaded(int level)
{
if (level == 3)
{
source.clip = level2Music;
source.Play ();
}
if (level == 0)
{
source.clip = level1Music;
source.Play ();
}
}
}
PauseMenu.cs - pause menu script
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
public class PauseMenu : MonoBehaviour {
public GameObject PauseUI;
public bool paused = false;
public GameObject loadingImage;
public GameObject OptionsMenuUI;
public GameObject SoundMenuUI;
//PauseUI toggle on/off items
public GameObject ResumeBtn;
public GameObject RestartBtn;
public GameObject OptionsBtn;
public GameObject QuitBtn;
public void Start()
{
PauseUI.SetActive (false);
}
//Pause Menu Trigger
public void Update()
{
if (Input.GetButtonDown ("Pause"))
{
paused = !paused;
}
if (paused)
{
PauseUI.SetActive (true);
Time.timeScale = 0;
//pause menu btns
ResumeBtn.SetActive (true);
RestartBtn.SetActive (true);
OptionsBtn.SetActive (true);
QuitBtn.SetActive (true);
}
if (!paused)
{
PauseUI.SetActive (false);
Time.timeScale = 1;
OptionsMenuUI.SetActive (false);
SoundMenuUI.SetActive (false);
}
}
//Button functions
public void Resume ()
{
paused = false;
}
public void Restart ()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
loadingImage.SetActive (true); //load image
}
public void MainMenu ()
{
SceneManager.LoadScene(0);
loadingImage.SetActive (true); //load image
}
public void OptionsPress ()
{
OptionsMenuUI.SetActive (true);
//PauseUI toggle on/off items
ResumeBtn.SetActive (false);
RestartBtn.SetActive (false);
OptionsBtn.SetActive (false);
QuitBtn.SetActive (false);
PauseUI.SetActive (false);
}
public void Quit ()
{
Application.Quit();
loadingImage.SetActive (true); //load image
}
}