简体   繁体   English

统一分步执行条件

[英]Executing conditions step-by-step in unity

When I execute the first if condition, it does not check the inside if condition.当我执行第一个 if 条件时,它不会检查内部 if 条件。 Why is this so?为什么会这样? I'm trying to put myself in a mind of a user.我试图把自己放在用户的脑海中。 They might choose b1 and then choose b2 later on.他们可能会选择 b1,然后再选择 b2。 Or they might choose b2 and then choose b1 later on.或者他们可能会选择 b2,然后再选择 b1。 However, when I tried to choose b1 first and then b2 second, it goes automatically to the third if condition which is when they choose b2 first.但是,当我尝试先选择 b1 然后再选择 b2 时,它会自动转到第三个 if 条件,即他们首先选择 b2 时。

private void OnTriggerEnter2D(Collider2D collision)
{
  if (collision.gameObject.CompareTag("b1"))
  {
     transform.GetChild(0).gameObject.SetActive(false);
     transform.GetChild(1).gameObject.SetActive(true);

     if (collision.gameObject.CompareTag("b2"))
     {
        transform.GetChild(1).gameObject.SetActive(false);
        transform.GetChild(3).gameObject.SetActive(true);
      }
  }

  if (collision.gameObject.CompareTag("b2"))
  {
     transform.GetChild(0).gameObject.SetActive(false);
     transform.GetChild(2).gameObject.SetActive(true);

     if (collision.gameObject.CompareTag("b1"))
     {
         transform.GetChild(2).gameObject.SetActive(false);
         transform.GetChild(3).gameObject.SetActive(true);
     }

}

When I execute the first if condition, it does not check the inside if condition当我执行第一个 if 条件时,它不会检查内部 if 条件

Because thats impossible.因为那是不可能的。 You're saying 'If the tag is b1, and if the tag is b2'.你说'如果标签是 b1,如果标签是 b2'。 The tag cannot be both, its either b1 or b2.标签不能同时是 b1 或 b2。

If the user chooses b2 at a later point, the method is called again and is executed from the top.如果用户稍后选择 b2,则再次调用该方法并从顶部执行。 It doesnt just continue where it left off last time.它不只是从上次中断的地方继续。

If you want this behaviour you'll have to add extra logic, for example keep track which tags have been collided with in the past.如果您想要这种行为,则必须添加额外的逻辑,例如跟踪过去与哪些标签发生冲突。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM