check if i'm collided with any objects of a certian tag?

I know how to use OnTriggerEnter and OnTriggerExit but when I exit one of the two area cubes (they have IsTrigger on them) it turns off a bool but I want if im not in either of the objects it turns off the bool.
(the player can touch the two areas at the same time) if I do OnTriggerExit it turns the bool off if I exit one of the two areas. So how to make it if i’m NOT touching both of the areas then itll turn a bool off?

If i understand correctly, you have 2 triggers, which are overlapping. So if you trigger both of them, at the same time and you exit only one, the bool that you have is turning OFF. So you want that boolean to turn OFF, only if you exit both triggers, is that correct? Your question is confusing…

If i am correct, then you can make another gameObject (Like a gameManager) with a script. Reference, that gameManager to both triggers and make one method, like

private bool theBool = false; // The bool, that you want to be switched

private int triggersToBeActivated = 2; // The number of triggers, that have to be active to make the boolean true
private int triggersActive = 0;

public void TriggerEntered()
{
      triggersActive ++;
      CheckTriggersState();
}

public void TriggerExited()
{
      triggersActive--;
      CheckTriggersState();
}

private void CheckTriggersState()
{
      if (triggersActive >0) // If one of the triggers is active
             theBool = true; // Activate the bool
      else if (triggersActive == 0)
             theBool = false;
}

Now, when you enter one of the triggers, the bool will became true. It will only became false if you exit both triggers.

P.S. i have wrote the script here, so it might have some errors. But i’m just showing a way for doing it…

You would achieve this like so:

void OnTriggerEnter(Collider col)
{
    if (col.gameObject.tag == "FirstArea")
    {
        FirstBool = true;
    }

    if (col.gameObject.tag == "SecondArea")
    {
        SecondBool = true;
    }
}

void OnTriggerExit(Collider col)
{

    if (col.gameObject.tag == "FirstArea")
    {
        FirstBool = false;
    }

    if (col.gameObject.tag == "SecondArea")
    {
        SecondBool = false;
    }
}

It’s untested and you’ll need to change the bool’s and object tag areas accordingly, but you should get the idea. Good luck!

This might do the trick. We store all objects with said tag in a list when we trigger OnTriggerEnter, then remove them when we trigger OnTriggerExit, finally, we check the list’s count for any objects left.

using System.Collections.Generic;
    
public class YourClassName: MonoBehaviour {
    
   List<GameObject> DetectedObjects = new List<GameObject>();
    
    void OnTriggerEnter(Collider other){
        if(other.tag == "YourTagHere" && 
           !DetectedObjects.Contains(other.gameObject)){
          DetectedObjects.Add(other.gameObject);
       }
    }
    
    void OnTriggerExit(Collider other){
       if(other.tag == "YourTagHere" &&
         DetectedObjects.Contains(other.gameObject)){
         DetectedObjects.Remove(other.gameObject);          
          if(DetectedObjects.Count == 0){
             //Your Code here when object is not touching any objects with said tag
          }
       }
    }
}

Couldn’t you just use something like this:

 void OnTriggerEnter (Collider col) {
        if (col.gameObject.CompareTag("TagOfYourChoice")) {
                  //If object that has been touched is the correct tag, do something.
        }
}

You can just put that on both of your objects. If you want them both to be touched in order to do something, you can try this.

bool Trigger1;
bool Trigger2;

void OnTriggerEnter (Collider col) {
         if (col.gameObject.CompareTag("TagOfYourChoice1")) {
                  Trigger1 = true;
         }
         
          if (col.gameObject.CompareTag("TagOfYourChoice2")) {
                  Trigger2 = true;
         }
}

void OnTriggerExit (Collider col) {
         if (col.gameObject.CompareTag("TagOfYourChoice1")) {
                  Trigger1 = false;
         }
         
          if (col.gameObject.CompareTag("TagOfYourChoice2")) {
                  Trigger2 = false;
         }
}

void Update () {
          if (Trigger1 && Trigger2) {
                     /If Object 1 and Object 2 are both being touched, Do something
           }
}

You could also just make them the same tag, but if you did that, only one object would have to be touched, not both.

If you want the same bool for both of the objects and you want it to turn of if either of the objects is exited then just make the two areas the same tag and make a bool.

bool entered;

void OnTriggerEnter (collider col) {
       if (col.gameObject.CompareTag("TagOfYourChoice")) {
                  entered = true;
        }
} 

void OnTriggerExit (collider col) {
       if (col.gameObject.CompareTag("TagOfYourChoice")) {
                  entered = false;
        }
}

void Update () {
        if (!entered) {
                   //Do something here if Colliders have been exited
         }

           if (entered) {
                   //Do something here if Colliders have been entered
         }

  }

If you use this last script, just make both of the gameObjects have the same tag and if one is exited, then the bool will say that they are all exited.

I hope that helped.