Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed May 20, 2016 at 04:26 AM by Gaming-Dudester for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Gaming-Dudester · May 17, 2016 at 01:05 AM · c#unity 5collisioncollider

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Le-Pampelmuse · May 17, 2016 at 03:02 AM 1
Share

Well, you most likely just need to add a new bool that turns false when both the bool's in the triggerscripts are false.

Arrange your if statements accordingly.

 if(triggerA.bool = false)
 {
     if(triggerB.bool = false)
     {
         mainBool = false;
     }
 }

4 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by instruct9r · May 17, 2016 at 01:32 AM

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...

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image instruct9r · May 20, 2016 at 11:35 AM 0
Share

Btw. I'm not sure, fi you want the bool to be activated, when you enter only of the triggers, or when you enter both at the same time.

If you want it to be active, when you enter both at the same time, then you can just set

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

Otherwise i think you don't need the triggersToBeActivated variable. So you can just remove it...

avatar image
0

Answer by S_Byrnes · May 17, 2016 at 01:34 AM

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!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image S_Byrnes · May 17, 2016 at 01:35 AM 0
Share

Wow, three answers all at once.

avatar image
0

Answer by LearningisFun · May 17, 2016 at 04:18 AM

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
           }
        }
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by aballif · May 17, 2016 at 04:16 AM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

169 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Weapon System with collide detection (Helps with script pls)!!! 0 Answers

Instantiate 1 object after 2 objects collide. ( C# ) 1 Answer

Fist Punch Collision 0 Answers

Mouse Movement with physics 1 Answer

OnCollisionEnter problem (RocketJump script) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges