- Home /
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).
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!
I was just about to google for this issue. Thank you for saving me an obscene amount of searching time!
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?
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?
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).
Wouldn't putting the player to "Never Sleep" cause performance issues?
@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.
Your answer
Follow this Question
Related Questions
Collision on specific Frames 1 Answer
Multiple Cars not working 1 Answer
[c#]collision script not working 1 Answer
Collider just for Tag (2D) (C#) 1 Answer
Destroy object on touch of object with specific class 3 Answers