[英]Bool not returning true in IF statement C# Unity
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;
}
}
}
I posted this earlier but due to lack of clarity (on my part) I can't figure out why this if statement 我在较早之前发布了此内容,但由于缺乏清晰性(我个人),我无法弄清楚为什么使用此if语句
if (leftChoice == true)
{
Debug.Log("Jesus Marty! you fixed it! Great Scott!");
}
isn't executing, the bool is being set to true on mousedown correct? 不执行,将bool设置为true在mousedown正确吗?
Any help would be greatly appreciated 任何帮助将不胜感激
Edit: as some people have pointed out in the comments, it may be (and probably is) the case that Update() is getting called before OnMouseDown(). 编辑:正如某些人在评论中指出的那样,可能是(可能是)在OnMouseDown()之前调用Update()的情况。 I'm going to assume nothing else calls these functions, so it may be worth calling ZombieCards() from your OnMouseDown() event instead. 我将假设没有其他函数调用这些函数,因此值得从您的OnMouseDown()事件中调用ZombieCards()。 That way you can be sure the sequence of events is right. 这样,您可以确定事件的顺序是正确的。 (End edit) (结束编辑)
Is there a reason you've set LeftChoice and RightChoice to public instead of private inside the class? 在类中将LeftChoice和RightChoice设置为public而不是private是有原因的吗? This generally means something outside the class could be accessing it. 通常,这意味着该类之外的内容可以访问它。 Given you're using Update(), I assume you're inheriting from MonoBehaviour, which means you're attaching this script as a component to another GameObject. 假定您使用的是Update(),我假设您继承自MonoBehaviour,这意味着您要将此脚本作为组件附加到另一个GameObject。 This means having private _leftChoice and private _rightChoice might be a better option here. 这意味着在这里拥有private _leftChoice和private _rightChoice可能是一个更好的选择。
Another thing which may help - Random.Range(lowInt, hightInt) will return a random int between (and including) lowInt and (highInt - 1), so in your code it will never choose 5. I believe the intention is to start from index 0 and do something like this: 另一件事可能会有所帮助-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;
...
}
Of course, you could also just do Random.Range(1, 6) 当然,您也可以只执行Random.Range(1,6)
Hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.