Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question How to disable Collider2D collision detection for multiple objects

Discussion in 'Physics' started by mukulabdagiri, Jan 22, 2024.

  1. mukulabdagiri

    mukulabdagiri

    Joined:
    Jul 1, 2016
    Posts:
    23
    Hi,
    I have a Brick breaker type of game in which all 'bricks' have a Collider2D component. When a ball collides with a brick i want to disable collisions for all the bricks temporarily. So i am doing
    Code (CSharp):
    1. for (int i = 0; i < bricksList.Count; i++)
    2. {
    3.    bricksList[i].GetComponent<Collider2D>().enabled = false;
    4. }
    when i detect ball-brick collision inside OnCollisionEnter2D.
    However it still detects collision for multiple bricks if ball hits them (in between) at the same time . This is messing up my calculations. I want to have collision for only one brick. How do i achieve that?
     
  2. mukulabdagiri

    mukulabdagiri

    Joined:
    Jul 1, 2016
    Posts:
    23
    I am just using a flag for now to detect if another collision happened with the same ball, but would like to know a better solution.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,689
    If you disable it, then all physics components are destroyed so you cannot get any collisions for it. TBH you're not really explaining the problem very well above, namely:

    What does this mean? Where you hit something has no bearing on whether you get a callback or not.

    You don't provide any information on how you're detecting hits now, you only mention OnCollisionEnter2D but not where that script is. Info such as what is a "brick"? A GameObject with its own collider and script with the above physics callback? A tilemap? etc etc.