Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Disabling multiple colliders

Discussion in '2D' started by bobueland, Jun 4, 2021.

  1. bobueland

    bobueland

    Joined:
    Sep 27, 2019
    Posts:
    19
    I have a huge number of obstacle, like bombs, lizard, snakes, spiders, lianes and so on. Some have polygon colliders, som circle colliders, some edge colliders and so on. The player has extra lives. When he collides with an obstacle and having extra lives, I want to subtract an extra life, and also to disable the collider of the object the player collided with. For instance if he collides with a bomb that has a circle collider attached to it I can write in my script

    other.gameObject.GetComponent<CircleCollider2D>().enabled = false;

    But I don't want to do that for every type of obstacle. Is there a way to generically turn off the collider, no matter what type of collider the obstacle has. Or would you do it some other way?
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    Not sure why you're asking for the GameObject of the Collider2D trigger you're hitting then asking for a specific collider type.

    "other" is the collider you hit so you just do "other.enabled = false" which is just a component disable, nothing to do with physics. Not sure why you only want to disable the collider but anyway, that's how you do it.

    NOTE: You can call GetComponent on any component; you don't need to do it on the GameObject.
     
  3. bobueland

    bobueland

    Joined:
    Sep 27, 2019
    Posts:
    19
    There seems to be some misunderstanding here about what other signifies. You seem to imply that

    Code (CSharp):
    1. void OnCollisionEnter2D(Collision2D other)
    2.     {
    3.         other.enabled = false;
    But then Unity complains and says

    error CS0200: Property or indexer 'Collision2D.enabled' cannot be assigned to -- it is read only

    Also you are trying to imply that I could use something like
    Code (CSharp):
    1. other.GetComponent<Bomb>().explode();
    But then I get the error
    error CS1061: 'Collision2D' does not contain a definition for 'GetComponent' and no accessible extension method 'GetComponent' accepting a first argument of type 'Collision2D' could be found (are you missing a using directive or an assembly reference?)

    On the other hand
    Code (CSharp):
    1. other.gameObject.GetComponent<Bomb>().explode();
    works without problems
     
  4. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,353
    So you could try using other.gameObject.GetComponent<Collider2D>().enabled = false;

    I believe that should work for any 2D collider type.

    Additionally, you could have the script on the enemy and do the reverse. When they collide with player or bomb or w/e, you disable the collider that way.

    One other way i just thought of if the generic Collider2D doesnt work, then you could just create a small function that checks what collider is present on that object and then disables it.

    like:
    Code (CSharp):
    1. private void CheckColliderTypeAndDisable()
    2.     {
    3.         if(gameObject.GetComponent<Collider2D>().GetType() == typeof(BoxCollider2D))
    4.         {
    5.             //Disable collider
    6.             return;
    7.         }
    8.         else if(gameObject.GetComponent<Collider2D>().GetType() == typeof(CircleCollider2D))
    9.         {
    10.             //Disable collider
    11.             return;
    12.         }
    13.  
    14.         //etc....
    15.     }
    It may be a bit ugly but its one way to have one function and disable any type of collider2D (if the above mentioned doesnt work)
     
    bobueland likes this.
  5. bobueland

    bobueland

    Joined:
    Sep 27, 2019
    Posts:
    19
     
    Cornysam likes this.
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    Yes, you didn't say what it was so I had to guess and guessed it was the Collider2D passed to the OnTriggerXXX2D callbacks. Now I know you mean the Collision2D.

    My point though and the link I gave shows that you can call "GetComponent" on a component and not only on a GameObject. Look at the docs for Collision2D; it shows that you get the GameObject but it also gives you the Collider2D and Rigidbody2D so you don't need to search.

    Code (CSharp):
    1. void OnCollisionEnter2D(Collision2D other)
    2. {
    3.    other.otherCollider.enabled = false;
    4. }
    Hope that helps.