Home > Enterprise >  Foreach loop skip objects
Foreach loop skip objects

Time:01-12

is it possible to skip the in active objects in the list.

iam making a bowling vr game. and the second time the bowling gets into the trigger zone that activate a foreach code that checks of the pawn has fallen. works perfectly fine intil you trow the second bowling ball. then i get the error that the in active pawns can't be find. is er possible he skips those?

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();
            }

        }

    }

CodePudding user response:

i found the solution

    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();
                }
            }

        }

CodePudding user response:

A clean way of writing it would be to first filter out the ones you want, then loop through the result

var activePawns = pawns.Where(x => x.gameOjbect.activeSelf);

foreach (var pawn in activePawns) 
{
    // Logic on active pawn
}

Keep in mind this may result in less performant code, as we loop through two lists, but often this will be a negliable performance hit.

Perhaps a better way, regardless, is to keep a list up to date with active pawns as they become deactivated, etc.

Or simply do like you did in your own answer, if you don't mind the if-statements!

  •  Tags:  
  • Related