Check if this 2D collider collides with a 2D collider in a list

Hello guys !

Here is my question :

  1. Moving the mouse will draw a line from the PreviousMousePosition to the CurrentMousePosition.
  2. I add an EdgeCollider2D to this line.

Now I would like to check if this EdgeCollider2D collides with one of the item from a List of GameObjects (These GameObjects have a BoxCollider2D)

What do I have to do ?
Thanks for reading

Edit: I would like to do this the Update(), I don’t want to use events. Do you think it is possible ?

c# - Do not copy paste. I wrote it “free” not in a text editor, there can be typos.

The only way I know to check if a collider collides with another one is by using void OnCollisionEnter.

You should put a tag to the GameObjects that you want to detect and then use this:

void OnCollisionEnter2D (Collision collision) {
    if(collision.collider.tag == "Tag of the objects"
    {
    // Do something
    }
    }

You can also use layers instead of tags if you want.

By the way it is bad to overload the Update function for the optimization of your game.

I found what to use to solve this problem.

I have to use Physics2D.Linecast()