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 /
avatar image
0
Question by fighder · Oct 14, 2016 at 02:38 AM · triggerphysics2dontriggerenterontriggerenter2d

Ontriggerstay2d only runs 26 times in a row?

So I was running some test about OnTriggerStay2D, and I realize it only runs 26 times in a row if no position changes are made between the two colliders involved.

Here is the code, I can't see anything wrong here:

 using UnityEngine;
 using System.Collections;
 
 public class TriggerTest : MonoBehaviour {
 
     public int i;
     public int a;
 
     void Update()
     {
         a = i * 2;
     }
 
     void OnTriggerStay2D(Collider2D other)
     {
         i++;
     }
 }

When something enters the trigger area, "i" goes up to 26 then stops, and "a" goes upto 52 (26*2) and stops. If I move the trigger area, or the collider inside the area, it will go up again, but stop after 26 times. I tried to run a debug.log as well, and the log only runs 26 times.

Anyone have any idea why this is like this?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by gtaharaedmonds · Oct 14, 2016 at 05:55 AM

Had this problem a while back. In the gameobject's rigidbody component, change the "sleeping mode" to "never sleep". This will have a hit on performance, though.

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 gtaharaedmonds · Oct 14, 2016 at 05:55 AM

Had this problem a while back. In the gameobject's rigidbody component, change the "sleeping mode" to "never sleep". This will have a hit on performance, though.

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 ElijahShadbolt · Oct 14, 2016 at 04:56 AM

Rigidbodies that aren't moving go into 'sleep mode', where they don't conduct physics updates to save processing power. This means OnTriggerStay2D and other Stay events get called for a short time, then the rigidbody goes to sleep so they no longer get called.

You can overcome this problem by only checking OnCollisionEnter/Exit, so that when an object enters the trigger it starts updating i and when it exits the trigger it stops updating i.

 using UnityEngine;
 
 public class TriggerTest : MonoBehaviour
 {
     public int i;
     public int a;
 
     private bool update_i = false;
 
     void Update()
     {
         if (update_i)
         {
             i++;
             a = i * 2;
         }
     }
 
     void OnTriggerEnter2D(Collider2D other)
     {
         update_i = true;
     }
 
     void OnTriggerExit2D(Collider2D other)
     {
         update_i = false;
     }
 }

In this script, i represents the number of frames that the collider is in the trigger.

You might also like to measure the change in time (in seconds) instead of frames. If so, you could change i to a float and update it like so: i += Time.deltaTime;

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

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

73 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

Related Questions

OnTriggerEnter2D not working, alternatives? 2 Answers

Why isnt OnTriggerEnter Tags working. 1 Answer

I am having a problem with detecting certain collision? 1 Answer

What is the difference between these two scripts? 1 Answer

OnTrigger event when colliders are already touching eachother 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