- Home /
Getting only one collider form OnTriggerEnter2D or OnCollisionEnter2D.
So I am making a top-down 2D game. In this game the character can interact with objects using a box collider that flashes on a key press using collider.enable/disable. It uses OnTriggerEnter2D() to send a "command" in a form of a boolian to the colliders. The problem is that it sends the command to all of the colliders instead of just one. I tried using Collider2D[] and accesing the first collider of the array, but that just gives an error "This message parameter has to be of type: Collider2D". So, how can I get only one object from OnTriggerEnter2D(), or maybe there is another way of doing it?
Post some code please - I'm having a really hard time understanding what you're doing. OnTriggerEnter2D is only going to give you one collider per call, but will be called for every collider that has entered the trigger. The only way to stop the call is by setting different physics layers to not interact, but that is not likely to be a solution to your problem. To have players interact with objects, you might want to take a look at the various "Cast" methods of Physics2d - BoxCast, LineCast, etc. They will make it much easier for you to select the right collider from all that were hit.
That Idea wasn't going to work from the beginning, I already got around it using raycast, but thanks for the replay anyways.
Your answer
![](https://koobas.hobune.stream/wayback/20220612083206im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
IsTouching and IsTouchingLayers for Colliders are not defined? 1 Answer
physics.OverlapSphere colliders 1 Answer
Trigger with get key down problems 0 Answers
Use Of OnCollisionStay()? 2 Answers
How to Specify a Collider in OnTriggerStay function? 1 Answer