Search Unity

Collider detecting collision inactive gameobject

Discussion in 'Physics' started by SprayNpraY, Dec 23, 2016.

  1. SprayNpraY

    SprayNpraY

    Joined:
    Oct 2, 2014
    Posts:
    156
    Hi All,

    Been following a tutorial on udemy one following side by side then re creating it myself in a separate project. I had everything working except the main character kept colliding with a coin that was set to inactive. I checked through the code several times it made no sense and then realised the coin on the working version (which I was following side by side of the tutorial) was on layer 1.

    The one I was recreating myself were on layer 0. As soon as I changed them to layer 1 the problem was solved. What I want to know is how having a gameobject that is set inactive will be detected by ontriggerenter2d on layer 0 but then it wouldn't in another.

    Is there a reason or is this a bug in unity itself?
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    Is the actual GameObject set as inactive? In that case it shouldn't be possible to collide with it. You shouldn't see it and it shouldn't interact with anything.

    On the other hand, if you disable components on a GameObject then they will still receive OnTrigger* and OnCollision* events. Since enable only stops certain events, such as Start, Update, and so on...

    All in all, it seems strange that it works in one layer, but not the other. Have you made changes to the Layer matrix? Are certain layers set to not interact with others?
     
  3. SprayNpraY

    SprayNpraY

    Joined:
    Oct 2, 2014
    Posts:
    156
    Hi PGJ thanks for the reply. I'm setting it to inactive using setactive(false) and for some very strange reason I was still colliding with the coin on trigger enter. the same happened with another game object that gives you more lives.

    Then for some reason changing them from layer 0 to 1 fixed it :/. Only changes made in collision matrix which was a while before was to do with what the backgrounds collide with.
     
  4. AlizaWenders

    AlizaWenders

    Joined:
    Jun 10, 2015
    Posts:
    2
    I know it's a "dead" topic, just wanted to second the OP. I'm using Unity 5.5.0f3 (don't know if it's fixed in later versions), and I have a consistent situation where a collider's GameObject is clearly disabled with .SetActive(), yet it still interacts in collisions (and runs the OnCollisionXXX functions attached to the disabled GameObject!!!). If I disable the collider component itself by setting it to .enabled = false, it deactivates as intended.
     
    CinnoMan likes this.
  5. CinnoMan

    CinnoMan

    Joined:
    Jan 27, 2014
    Posts:
    16
    I've just run into the same issue in Unity 2017.3 where a component on an inactive GameObject consistently reacts to OnTriggerStay2D. maybe it's something to do with the GameObject being set inactive OnTriggerStay2D in the same frame. My way to work around it is to check isActiveAndEnabled in OnTriggerStay2D (which shouldn't be necessary according to the documentation).
     
  6. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    Disabling components only pauses Start(), Update(), and FixedUpdate(). Collisions still occur, since these might be used to wake the component up again.
     
    MGGDev likes this.
  7. W4rf4c3

    W4rf4c3

    Joined:
    Jan 22, 2016
    Posts:
    13
    Yeah but even with GetComponent<Collider>().enabled = false or GetComponent<Collider2D>().enabled = false, the collision is still triggered. Disabling the component should kill its function no?
     
  8. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    I did a quick test:

    Code (CSharp):
    1.     private void OnCollisionStay2D(Collision2D collision)
    2.     {
    3.         Debug.Log("Hello");
    4.  
    5.         GetComponent<Collider2D>().enabled = false;
    6.     }
    And it worked as expected.
     
  9. killdevilproductions

    killdevilproductions

    Joined:
    Apr 15, 2021
    Posts:
    1
    I know this is a dead topic but If someone stumbles across this in the future I hope I helped. I thought the gameObject was interacting with other colliders, but it turn out my colliders offset was changed causing it to interact other colliders sometime with other colliders in the level, not because of any bugs with colliders still being active when the gameObjects are disable. So you might wannac heck if the offset for the collider changed
     
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,475
    Sorry but that makes no sense. If the GameObject is inactive then no physics objects will be on it or any children. Offsets or any other settings will make no difference.
     
    aleksandar2392 likes this.