Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Lolgab123 · Jan 19, 2015 at 02:45 AM · collidertriggerontriggerenterontriggerstay

Inverse of OnTriggerStay?

Hey guys! All is in the question: is there a way to do the inversion of OnTriggerStay? Like a function that will return something if there's absolutly NO objects on trigger?

Thanks

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Kiwasi · Jan 19, 2015 at 04:52 AM

There are a couple of options. If the collider is a sphere you can simply use OverlapShere to check for the presence of any colliders.

The other way is to keep track of all of the present colliders by adding them to a collection with OnTriggerEnter and removing in OnTriggerExit.

Comment
Add comment · Show 5 · 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 GameVortex · Jan 19, 2015 at 09:02 AM 0
Share

Alternatively you can also use OnTriggerStay together with the Update to keep track of if there has been any collisions:

 private bool inTrigger = false;
 
 private void Update()
 {
     if(!inTrigger)
     {
         //No objects in trigger
     }
     inTrigger = false;
 }
 
 private void OnTriggerStay()
 {
     inTrigger = true;
 }
avatar image Lolgab123 · Jan 19, 2015 at 11:49 AM 0
Share

The problem here is that the inTrigger variable will always switch between true and false because the Update function will always turn it back to false...

avatar image Lolgab123 · Jan 19, 2015 at 11:51 AM 0
Share

Can you show me an example of keeping track of the colliders? I tried working with arrays but it hust didn't works

avatar image Kiwasi · Jan 21, 2015 at 02:20 AM 0
Share
 List<GameObject> myObjects = new List<GameObject>();
 
 void OnTriggerEnter (Collider other){
     myObjects.Add(other.gameObject);
 }
 
 void OnTriggerExit (Collider other){
     myObjects.Remove(other.gameObject);
 }
avatar image Lolgab123 · Jan 21, 2015 at 02:36 AM 0
Share

Who, thanks!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to use OnTriggerEnter with multiple triggered objects? 1 Answer

OnTrigger event when colliders are already touching eachother 1 Answer

OnTriggerEnter fails to activate 1 Answer

Trigger Spawning? 1 Answer

Interpolation through a Trigger at runtime 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