how to make objects only collide with some objects Unity c#

i have two balls each with a separete rigidbody bouncing around in a 2D space, and i want them to collide with everything except the other ball.
how do i do that?

You can put the two balls in separate layers, and then in the Physics2DSettings “Layer Collision Matrix” disable interaction between the two layers (uncheck the checkbox):

  1. Edit Menu/Project Settings/Tags and Layers
  2. Expand the Layers section at the bottom
  3. In two “User Layers” add: “BallLayer1” and “BallLayer2”
  4. Edit Menu/Project Settings/Physics 2D
  5. If necessary, expand the “Layer Collision Matrix”
  6. “BallLayer1” and “BallLayer2” will be listed both along the top and on the left hand side
  7. Identify where these two layers interest and uncheck the checkboxes
  8. All other checkboxes should be checked
  9. Assign “BallLayer1” and “BallLayer2” to Ball 1 and Ball2 respectively (top right in the Inspector of each ball)

That’s it! The two balls will interact with everything else (normally in the Default Layer), except each other.

thanks, worked perfectly.