- Home /
OnTriggerEnter2D is called in two different objects in the same frame.
Hello coders, I am developing a 2D game. I have many trigger colliders, let's say 4, that are quite near each other but they do not touch. They are static. I have another collider, let's say a ball, which is the one I want to detect with these triggers and when they do they add a point. The problem is that sometimes it happens that this collider I want to detect touches TWO of the triggers in the same frame, making the OnTriggerEnter2D be called for two different triggers; and this is going to bug my game. I cannot use (normal) flag variables because the functions are called together, in the same frame.
This is some of the debug (reason why I am saying they touch in the same frame):
Time of entrance in Black Hole 3 Blue :6.32
Time of entrance in Black Hole 2 Green :6.32
Time of entrance in Black Hole 3 Blue :7.1
Time of entrance in Black Hole 2 Green :7.1
Time of entrance in Black Hole 3 Blue :7.16
Time of entrance in Black Hole 2 Green :7.16
The values at the same times are caused by the same collider touching two triggers ( black hole "blue" and "green" ).
I am sorry I do not really understand. What is a point creation method? You mean where the collider I want to detect is created from? I am creating it from a singleton class, a game manager. The triggers ins$$anonymous$$d are made with the editor and they're static throughout all the game.
you said your script adds a point on collision, i was just thinking if that was being done from an out side script (like a game manager) then you could possibly just use booleans to see if both triggers are active at any given frame, before your next bit of code.
Answer by Paricus · May 27, 2017 at 10:27 AM
https://postimg.org/gallery/2i3t984ea/4f750546/ https://postimg.org/image/qym8blvan/94b00706/
is this what you're trying to do
Answer by FM-Productions · May 27, 2017 at 03:32 PM
Have you tried to use a flag? Even if the functions are called in the same frame, the will have a certain execution order. I do not believe that there is any concurrent code in your game logic.
What you can additionally do: Create a gameObject storing the collision object:
GameObject firstColliderObject;
At the start of the game, set it to null (in Start() or Awake()). Then if an OnTriggerEnter2D function is triggered, check if firstColliderObject is still null, if so, set it to the gameObject of the collider.
void OnTriggerEnter2D(Collider2D other) {
if (firstColliderObject == null){
firstColliderObject = other.gameObject;
}
}
At the end of the update, you will know if there was a collision in this frame by checking the value of the firstColliderObject. If it is null, there was no collision. If it is not null, you have the first collided object. remember to reset firstColliderObject to null at the end of your update function, not at the start (I don't know if a OnTriggerEnter2D function is invoked inbetween the update function).
In the end of your update function (or basically anywhere in your update function) you can then use this code:
if (firstColliderObject != null){
//do something with firstColliderObject
}
firstColliderObject = null;
Answer by tMahon · May 27, 2017 at 04:59 PM
Have you tried using the Observer pattern? Just trigger an event from both and if the number of events is divisible by 2 execute the last event.