【发布时间】:2020-05-10 16:34:13
【问题描述】:
我刚刚在我的小测试游戏中引入了暂停选项。我在背景中有音频(在整个游戏中播放,即使场景发生变化),所以我决定在游戏暂停时停止音乐。出于某种原因,它运行良好,直到游戏重新加载/更改场景。
然后弹出一个错误
“‘AudioSource’类型的对象已被销毁。”
有人可以帮忙吗?另外..我认为它是一个组件,而不是一个对象!可能两者都有,我不确定。
我如何让我的音乐连续播放:
void Awake()
{
if (instance != null)
{
Destroy(gameObject);
}
else
{
instance = this;
GameObject.DontDestroyOnLoad(gameObject);
}
}
我的暂停菜单:
public static bool GameIsPaused = false;
public GameObject pauseMenuUI;
public AudioSource song;
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
if (GameIsPaused)
{
Resume();
}
else
{
Pause();
}
}
}
void Resume()
{
pauseMenuUI.SetActive(false);
Time.timeScale = 1f;
GameIsPaused = false;
song.mute = false;
}
void Pause()
{
pauseMenuUI.SetActive(true);
Time.timeScale = 0f;
GameIsPaused = true;
song.mute = true;
}
谢谢!
【问题讨论】: