Unity3D只调用单个if语句

时间:2017-07-07 04:40:27

标签: c# android unity3d

我的代码有问题。 只调用第一个if语句。 我知道我的等级正在增加,但是在调用第一个语句并且等级增加后,即使分数低于要求,仍然会显示继续按钮。

private void PlayBuzzer()
{
    GetComponent<AudioSource>().PlayOneShot(buzzer);
    timeRemaining -= 1;
    endPanel.SetActive(true);
    if (PlayerScore.score >= 15 && level == 1) // THIS ONE WORKS
    {
        GameObject.Find("Restart").SetActive(false);
        GameObject.Find("Continue").SetActive(true);
    }
// EVERYTHING BELOW HERE DOES NOT - Score Below 35 still shows 'Continue'
    else if (PlayerScore.score >= 35 && level == 2)
    {
        GameObject.Find("Restart").SetActive(false);
        GameObject.Find("Continue").SetActive(true);
    }
    else if (PlayerScore.score >= 45 && level == 3)
    {
        GameObject.Find("Restart").SetActive(false);
        GameObject.Find("Continue").SetActive(true);
    }
    else if (PlayerScore.score >= 70 && level == 4)
    {
        GameObject.Find("Restart").SetActive(false);
        GameObject.Find("Continue").SetActive(true);
    }
    else if (PlayerScore.score >= 120 && level == 5)
    {
        GameObject.Find("Restart").SetActive(false);
        GameObject.Find("Continue").SetActive(true);
    }
    else if (PlayerScore.score >= 160 && level == 6)
    {
        GameObject.Find("Restart").SetActive(false);
        GameObject.Find("Continue").SetActive(true);
    }
    else if (PlayerScore.score >= 230 && level == 7)
    {
        GameObject.Find("Restart").SetActive(false);
        GameObject.Find("Continue").SetActive(true);
    }
    else if (PlayerScore.score >= 300 && level == 8)
    {
        GameObject.Find("Restart").SetActive(false);
        GameObject.Find("Continue").SetActive(true);
    }
    else
    {
        GameObject.Find("Restart").SetActive(true);
        GameObject.Find("Continue").SetActive(false);
    }
    Time.timeScale = 0;
}

1 个答案:

答案 0 :(得分:0)

好的,不知道PlayBuzzer()的调用位置,如果在此之前或之后增加了级别,找到问题真的很难。你调试了程序吗?

无论如何,我采取了缩短你职能的自由,以遵循DRY原则:

private void PlayBuzzer()
{
    GetComponent<AudioSource>().PlayOneShot(buzzer);
    timeRemaining -= 1;
    endPanel.SetActive(true);
    var scores = new List<int> { 15, 35, 45, 70, 120, 160 };
    var mustRestart = PlayerScore.score < scores[level - 1];
    GameObject.Find("Restart").SetActive(mustRestart);
    GameObject.Find("Continue").SetActive(!mustRestart);
    Time.timeScale = 0;
}

您必须确保您的等级介于1和scores.Count之间。