OnTriggerStay2D skipped every 25th frame? -- collider turns light green
I have an OnTriggerStay2D that updates a frame variable used by another classes Update function. The frame variables are reset to default values in every LateUpdate. The issue I am running into is that every 25th frame OnTriggerStay2D is not called while Update() is called.
I looked at it in the editor and I noticed that the trigger region becomes duller for the 25th frame.
Do you know what I can do to ensure that OnTriggerStay2D is called every frame when it is triggered?
Answer by UnityCoach · Nov 10, 2018 at 11:53 AM
As it's physics related, OnTriggerStay2D() is called along FixedUpdate, not Update.
What you can do is store every object entering/exiting the trigger in a List, and do things in Update.
List<Collider2D> colliders = new List<Collider2D>();
void OnTriggerEnter2D (Collider2D other)
{
colliders.Add(other);
}
void OnTriggerExit2D (Collider2D other)
{
colliders.Remove(other);
}
void Update ()
{
foreach (Collider col in colliders)
{
// do what you want with every collider here
}
}
Your answer
Follow this Question
Related Questions
WheelJoint2D rotates parent object 1 Answer
Getting rid of 2D collision jitter 1 Answer
Help 2D Custom HillClimbRacingGame 0 Answers
2D Accuracy 0 Answers
Player controlled platforms 0 Answers