【发布时间】:2018-08-08 16:45:09
【问题描述】:
所以我正在开发一款跑步游戏,它几乎完成了。问题是我正在测试暂停面板,当玩家触摸僵尸时,暂停面板出现,我通过按下重新启动按钮再次重新启动游戏。但是当我再次触摸僵尸面板时,面板没有出现,并给了我标题中的错误。我被卡住了,任何帮助将不胜感激。这是代码,我引用了错误发送给我的行:
[SerializeField]
private GameObject pausePanel;
[SerializeField]
private Button RestartGameButton;
[SerializeField]
private Text ScoreText;
private int score;
void Start ()
{
pausePanel.SetActive(false);
ScoreText.text = score + "M";
StartCoroutine(CountScore());
}
IEnumerator CountScore()
{
yield return new WaitForSeconds(0.6f);
score++;
ScoreText.text = score + "M";
StartCoroutine(CountScore());
}
void OnEnable()
{
PlayerDeath.endgame += PlayerDiedEndTheGame;
}
void OnDisable()
{
PlayerDeath.endgame += PlayerDiedEndTheGame;
}
void PlayerDiedEndTheGame()
{
if (!PlayerPrefs.HasKey("Score"))
{
PlayerPrefs.SetInt("Score", 0);
}
else
{
int highscore = PlayerPrefs.GetInt("Score");
if(highscore < score)
{
PlayerPrefs.SetInt("Score", score);
}
}
pausePanel.SetActive(true); //this is the line that error sends me but I cant figure it out because I didnt try to destroy the panel in the first place.
RestartGameButton.onClick.RemoveAllListeners();
RestartGameButton.onClick.AddListener(() => RestartGame());
Time.timeScale = 0f;
}
public void PauseButton()
{
Time.timeScale = 0f;
pausePanel.SetActive(true);
RestartGameButton.onClick.RemoveAllListeners();
RestartGameButton.onClick.AddListener(() => ResumeGame());
}
public void GoToMenu()
{
Time.timeScale = 1f;
SceneManager.LoadScene("MainMenu");
}
public void ResumeGame()
{
Time.timeScale = 1f;
pausePanel.SetActive(false);
}
public void RestartGame()
{
Time.timeScale = 1f;
SceneManager.LoadScene("Gameplay");
}
【问题讨论】:
-
这一行是什么:
PlayerDeath.endgame += PlayerDiedEndTheGame;?第二个参数是一个函数,考虑到我假设 PlayerDeath.endgame 是某种数字类型,这令人困惑。您的意思是在分号前添加()? -
那个函数也不返回任何东西......不知道你想在那里做什么
-
残局是一个事件。我声明了一个名为 EndGame 的委托 void,并在其上添加了一个同名 endgame 的事件。该类基本上指定了死亡条件。如果玩家离开屏幕或触摸僵尸,他们就会死亡并出现事件。
-
您正在重新加载一个全新的场景。我不知道您是如何设置
pausePanelGameObject 的,但它在加载时会被破坏。您需要,应该,为此菜单使用 DontDestroyOnLoad。另外,我认为您想在OnDisable方法中删除 (-)PlayerDiedEndTheGame。 -
是的,这就是错误。当我仔细检查方法时我意识到了这一点,但我忘记了更改 OnDisable 方法上的 + 号。当我输入 - 时它起作用了。感谢您的帮助。