Bool在IF语句C#Unity中没有返回true

时间:2017-01-28 21:15:01

标签: c# if-statement unity3d boolean instantiation

GameObject ZombieCard1, ZombieCard2, ZombieCard3, ZombieCard4, ZombieCard5;
int randomnumberpicker;
public static bool leftChoice, rightChoice;
void Start()
{

    statusBars = FindObjectOfType<StatusBars>();
}


void Update()
{
    ZombieCards();

}

void ZombieCards()
{
    if (GameObject.FindWithTag("Card") == null)
    { 
     randomnumberpicker = Random.Range(1, 5);
     Debug.Log(randomnumberpicker);
     if(randomnumberpicker == 1)
        {
           ZombieCard1 = Instantiate(Resources.Load("Frontcard1")) as GameObject;
           ZombieCard1.transform.Translate(0, -1, 0);
            if (leftChoice == true)
            {
                Debug.Log("Jesus Marty! you fixed it! Great Scott!");
            }
        }
     else if(randomnumberpicker == 2)
        {
            ZombieCard2 = Instantiate(Resources.Load("Frontcard2")) as GameObject;
            ZombieCard2.transform.Translate(0, -1, 0);
        }
     else if (randomnumberpicker == 3)
        {
            ZombieCard3 = Instantiate(Resources.Load("Frontcard3")) as GameObject;
            ZombieCard3.transform.Translate(0, -1, 0);
        }
     else if (randomnumberpicker == 4)
        {
            ZombieCard4 = Instantiate(Resources.Load("Frontcard4")) as GameObject;
            ZombieCard4.transform.Translate(0, -1, 0);
        }
     else if (randomnumberpicker == 5)
        {
            ZombieCard5 = Instantiate(Resources.Load("Frontcard5")) as GameObject;
            ZombieCard5.transform.Translate(0, -1, 0);
        }
    }
}
 void OnMouseDown()
    {
        if (gameObject.CompareTag("LeftArrow"))
        {
            leftChoice = true;
            Debug.Log("Left arrow is working");
            return;
        }
        if (gameObject.CompareTag("RightArrow"))
        {
            rightChoice = true;
            Debug.Log("Right arrow is working");
            return;
        }
    }
}

我之前发布过这篇文章,但由于缺乏清晰度(就我而言),我无法弄清楚为什么这个if语句

if (leftChoice == true)
        {
            Debug.Log("Jesus Marty! you fixed it! Great Scott!");
        }

没有执行,在mousedown上bool被设置为true吗?

非常感谢任何帮助

1 个答案:

答案 0 :(得分:1)

编辑:正如有些人在评论中指出的那样,在OnMouseDown()之前调用Update()可能是(也可能是)。我将假设没有其他任何调用这些函数,因此可能需要从OnMouseDown()事件中调用ZombieCards()。这样你就可以确定事件的顺序是正确的。 (结束编辑)

你有没有理由在课堂上将LeftChoice和RightChoice设置为public而不是private?这通常意味着课外的东西可以访问它。鉴于您正在使用Update(),我假设您继承了MonoBehaviour,这意味着您将此脚本作为组件附加到另一个GameObject。这意味着私有_leftChoice和private _rightChoice可能是更好的选择。

另一件可能有用的事情 - Random.Range(lowInt,hightInt)将在lowInt和(highInt - 1)之间返回一个随机int,所以在你的代码中它永远不会选择5.我相信的意图是从索引0开始并执行以下操作:

    private const bool NumChoices = 5;
    ...
    int randomNumberPicker = Random.Range(0, NumChoices);
    switch (randomNumberPicker)
    {
        case 0:
            ZombieCard1 = Instantiate(Resources.Load("Frontcard1")) as GameObject;
            ZombieCard1.transform.Translate(0, -1, 0);
            if (leftChoice == true)
            {
                Debug.Log("Jesus Marty! you fixed it! Great Scott!");
            }
            break;
        ...
    }

当然,你也可以做Random.Range(1,6)

希望这有帮助!