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 amostajo · Jul 27, 2013 at 12:22 AM · triggerontriggerstay

Is there a way to turn off OnTriggerStay?

I need to call OnTriggerStay only once if specific conditions are reached. I have created a script that calls OnTriggerStay and disables the script once the message is sent, but it seems that even though the script is disabled OnTriggerStay is still being called by Unity.

I don't want to disable the collider because I need to use OnTriggerEnter and OnTriggerExit for other purposes, both functions are in a separated script apart from the one with OnTriggerStay. Disabling and enabling the collider causes CPU spikes.

Any ideas?

 public function OnTriggerStay (collider : Collider) {
   if (!this.enabled) {
     Debug.Log("Hi, Im disabled");
   }
   // Some code TODO
   this.enabled = false;
 }
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
1
Best Answer

Answer by aldonaletto · Jul 27, 2013 at 12:33 AM

Yes, disabling a script only stops the periodic functions (Update, LateUpdate, FixedUpdate) - events called by the collision system still are active.

A simple (and faster) way to avoid OnTriggerStay is to set a boolean variable at OnTriggerEnter and clear it at OnTriggerExit, and call the desired code inside Update or FixedUpdate when this variable is true - like this:

 bool inTrigger = false;
 
 void OnTriggerEnter(Collider other){
   inTrigger = true;
   // other code
 }
 
 void OnTriggerExit(Collider other){
   inTrigger = false;
   // other code
 }
 
 void Update(){
   if (inTrigger){
     // OnTriggerStay code
   }
   // other code
 }

There's a problem, however: if this code is attached to the trigger and two objects enter it, the first to enter sets the variable and the first to exit clears it, thus the second object doesn't do anything when exiting. A possible solution is to count events instead, like below:

 int inTrigger = 0;
 
 void OnTriggerEnter(Collider other){
   inTrigger++;
   // other code
 }
 
 void OnTriggerExit(Collider other){
   inTrigger--;
   // other code
 }
 
 void Update(){
   if (inTrigger > 0){
     // OnTriggerStay code
   }
   // other code
 }

This way, the variable inTrigger is > 0 when there are objects inside the trigger, and only returns to zero when all of them left it.

Comment
Add comment · Show 2 · 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 amostajo · Jul 27, 2013 at 01:51 AM 0
Share

Excellent solution.

Yup this will definitely work for me.

avatar image ChunkyToads · Feb 14, 2020 at 11:37 PM 0
Share

Thanks for kindly sharing your knowledge.... searched for nearly a day for this. It clearly and simply explained caveats as well. Anyway thank you! :)

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

17 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

Related Questions

Determine that an object is -within- a trigger 1 Answer

Detect when a trigger has entered a collider 1 Answer

OnTriggerStay Question 0 Answers

Getting triggers to stop triggering on some objects once certain criteria is met 1 Answer

Call function on all objects in trigger 2 Answers


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