Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
11
Question by Zenatsu · May 26, 2015 at 06:28 AM · c#2dcollision

OnTriggerStay2D stops working randomly.

I am using OnTriggerStay2D to handle damage between 2 objects. However, randomly the function will stop firing altogether. It consistently works, however when it stops working seems random. I don't understand what could cause it to stop firing.

Here is the code for the OnTriggerStay function:

 void OnTriggerStay2D(Collider2D collider)
     {
         if (collider.gameObject.tag == "BadGuy") //Stop moving and fight!
         {
             this.gameObject.GetComponent<GoodGuy>().speed = 0;
             
             collider.gameObject.GetComponent<BadGuy>().currentHealth -= this.gameObject.GetComponent<GoodGuy>().baseAttack;
         }
         
     }


Further testing has shown that it seems to stop between 4 and 6 when counting down from 20. Which makes me wonder if OnTriggerStay2D only calls a certain number of times before stopping? Yet using a for loop, it'll go through the iterations completely (like 0-30).

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
83
Best Answer

Answer by Zenatsu · May 26, 2015 at 03:35 PM

Solution!

Turns out my specific problem ended up being the fact that they were falling asleep. Lazy jerks.

The solution to my problem was as simple as changing "Sleeping Mode" within the Rigidbody2D component to "Never Sleep". This will allow onTriggerStay2D to continue it's co-routine until either of the objects have been destroyed (I.E health is 0), is manually put into a sleep state, or exits the collision (which calls onTriggerExit2D).

Alternatively, going to Edit -> Project Settings -> Physics 2D, you can edit how long until objects are automatically put to sleep under the "Time To Sleep" parameter.

So many hours were wasted trying to figure this hurdle out. But I still came out of this more knowledgeable!

Comment
Add comment · Show 7 · 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 ChuckIes · Jan 29, 2016 at 01:48 AM 0
Share

I was just about to google for this issue. Thank you for saving me an obscene amount of searching time!

avatar image Magnumstar · Mar 25, 2016 at 02:12 AM 0
Share

Why does this matter? I was using OnTriggerStay2D and it worked for a few weeks. I kept tweaking my game and all of a sudden it didn't work unless I moved the character in and around the triggered collider. I followed your advice and then it worked again. what happened? I need to know why?

avatar image ifndef · Nov 06, 2016 at 08:44 PM 0
Share

Another frustrating 5 hour search turned into a quick informative read on how unity works. Thanks a lot!

Do they put it to sleep (by default) because of performance issues?

avatar image lucassaldanha · Apr 09, 2021 at 10:49 AM 0
Share

Thank you so much for sharing this solution. Another option that I have found is to change the Rigidbody option "Sleeping Mode" to "Never Sleep". This seems to achieve the same effect without changing it globally.

I did the change in the Rigidbody that is being evaluated (e.g. the BadGuy Rigidbody).

avatar image jaypordy · Jun 03, 2021 at 09:47 PM 0
Share

Wouldn't putting the player to "Never Sleep" cause performance issues?

avatar image lucassaldanha jaypordy · Jun 04, 2021 at 02:38 AM 0
Share

@jaypordy It will use more processing, but if this will cause issues it depends on the use-case and the number of objects that are being included in the calculation. You can always test it and use the monitoring tools to see the performance impact on the game.

Show more comments

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

27 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

Related Questions

2D box colliders not touching but are colliding, how to fix? 0 Answers

Distribute terrain in zones 3 Answers

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

Why is my collision script not working? 1 Answer

OnTriggerStay2D only detecting trigger collisions while moving 3 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