【发布时间】:2017-01-28 21:15:01
【问题描述】:
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!");
}
没有执行,bool 在 mousedown 时被设置为 true 对吗?
任何帮助将不胜感激
【问题讨论】:
-
你可以很确定,如果
leftChoice是true,你会输入整个if块... -
在你有机会触发
OnMouseDown()之前不会调用ZombieCards(),导致它实例化一张卡片(我认为这会创建一个带有标签“卡片”的对象)并导致它再也不会在ZombieCards()中输入最初的 if 语句了吗?请描述您希望发生的事件顺序,因为我认为您的代码中可能存在逻辑错误。 -
只是好奇。您在何时何地将这些全局静态变量设置回 false?
-
@Serlite 我希望 bool 触发创建的对象内的事件,如您所说,用 Card 标记。我该怎么办?我理解我的逻辑中的缺陷,但最好的工作是什么。
-
@MatíasFidemraizer 你为什么这么说?
标签: c# if-statement unity3d boolean instantiation