【问题标题】:Foreach loop skip objectsForeach 循环跳过对象
【发布时间】:2022-01-11 10:52:27
【问题描述】:

是否可以跳过列表中的活动对象。

我正在制作保龄球虚拟现实游戏。保龄球第二次进入触发区域时,会激活一个检查棋子的 foreach 代码已经下降。在你投下第二个保龄球之前工作得很好。然后我得到了找不到活动棋子的错误。他有可能跳过那些吗?

public void PawnHasFallen()
    {
        Debug.Log("Calling this script");

        //see of the pawn has moved after the wait in the other script
        if (!Mathf.Approximately(Vector3.Angle(Vector3.up, transform.up), 0f))
        {
            StartCoroutine(PawmEnumerator());
            Debug.Log(pawnName.name + " has fallen", this);
            //function
        }
        else
        {
            Debug.Log(pawnName.name + " is still standing", this);
            //fuint
        }

       
    }

    IEnumerator PawmEnumerator()
    {
        if (pawnName.activeInHierarchy == true)
        {
            rb.velocity = Vector3.up * trustFroce * Time.deltaTime;
            yield return new WaitForSeconds(2);
            rb.constraints = RigidbodyConstraints.FreezeAll;
            Boom();
            yield return new WaitForSeconds(0.5f);
            pawnName.gameObject.SetActive(false);
        }
    }

    public void Boom()
    {
        foreach (var ps in psPawn)
        {
            ps.Play();
        }
    }

public IEnumerator OnTriggerEnter(Collider collision)
    {
        Debug.Log("Big balls");
        //if the ball hits the end of the bowling ally start this 
        if (collision.gameObject.tag == "Ball")
        {
            Debug.Log("Start de second wait");
            //wait 5 second for starting the fuction
            yield return new WaitForSeconds(5);
            Debug.Log("Ended the waiting");
            //this function sees if the pawn has fallen and then gives you points
            //pawn.PawnHasFallen();
            foreach(var pawn in pawns)
            {
                pawn.PawnHasFallen();
            }

        }

    }

【问题讨论】:

    标签: unity3d foreach virtual-reality


    【解决方案1】:

    我找到了解决办法

        public IEnumerator OnTriggerEnter(Collider collision)
        {
            Debug.Log("Big balls");
            //if the ball hits the end of the bowling ally start this 
            if (collision.gameObject.tag == "Ball")
            {
                Debug.Log("Start de second wait");
                //wait 5 second for starting the fuction
                yield return new WaitForSeconds(5);
                Debug.Log("Ended the waiting");
                //this function sees if the pawn has fallen and then gives you points
                //pawn.PawnHasFallen();
                foreach(var pawn in pawns)
                {
                    if (!pawn.gameObject.activeSelf)
                    {
                        Debug.Log("do nothing");
                    }
                    else
                    {
                        pawn.PawnHasFallen();
                    }
                }
    
            }
    

    【讨论】:

      【解决方案2】:

      一种简洁的编写方式是首先过滤掉你想要的,然后循环遍历结果

      var activePawns = pawns.Where(x => x.gameOjbect.activeSelf);
      
      foreach (var pawn in activePawns) 
      {
          // Logic on active pawn
      }
      

      请记住,当我们遍历两个列表时,这可能会导致代码性能下降,但这通常对性能造成的影响可以忽略不计。

      无论如何,也许更好的方法是在活动棋子被停用时保持列表的最新状态,等等。

      如果您不介意 if 语句,或者只是像您在自己的答案中那样做!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-05-20
        • 1970-01-01
        • 2021-01-08
        • 2021-02-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-14
        相关资源
        最近更新 更多