Check if an object leaves the Physics.OverlapBox

So, I am pretty new to this. But I know the overlap collects all colliders into an array however it also removes the items in that array after leaving. I am grabbing these objects and putting them in a list to check if its colliding with it, but the problem is when the objects leave, they don’t leave the list or update the bools to false. Is there a way for me to check if the specific object is no longer inside the overlap sphere or am I going to have to use overlap and Box Collider

        //Grabs all collisions
        size = new Vector3(halfX, halfY, halfZ);
        center = new Vector3(transform.position.x, transform.position.y, transform.position.z);
        collisions = Physics.OverlapBox(center, size/2, Quaternion.identity);
        int i = 0;
        while(i < collisions.Length)
        {
            //Debug.Log("Collided " + collisions[i].tag + i);
            i++;
        }

        Transform[] childTransform = parent.GetComponentsInChildren<Transform>();
        foreach(Transform child in childTransform)
        {
            if(!children.Contains(child))
            {
                //Keeps from adding the children colliders in the list
                children.Add(child);
            }
        }

        foreach(Collider col in collisions)
        {
            if(!children.Contains(col.transform))
            {
                if(!obj.Contains(col.gameObject))
                {
                    //Adds the actual collisions to the list
                    obj.Add(col.gameObject);
                }
            }
        }

Is it possible for you to use OnTriggerEnter and OnTriggerExit? Then you have a reference to the leaving object.
Otherwise you have to compare the old array/list and the new one to see which are missing

Well, I wanted to avoid triggers and colliders because it picks up everything inside the room and lags my computer. With the overlap I’m able to keep certain objects from being picked up. I basically just wanted a more finetuned collider and this is the only way I can think of.

I think I will look more into this. I did not know you could compare them. Thank you.

You can simply use layers and the collision matrix and it will just collide what you want. It’s probably more performant or very close to it.

That, and comparing collections usually is slow. See the implementation here: https://stackoverflow.com/questions/3944803/use-linq-to-get-items-in-one-list-that-are-not-in-another-list

So i’ve been overthinking it this whole time. Damn, okay. I’m going to switch back to colliders then.

1 Like