Search Unity

Collision is still detected on a destroyed object [SOLVED]

Discussion in 'Scripting' started by jzzja, Jan 17, 2019.

  1. jzzja

    jzzja

    Joined:
    Sep 28, 2017
    Posts:
    43
    Here is what is happening:

    object 1 is on top of object 2 and I am using onCollision, onCollisionStay and OnCollisionExit to register the collisions between the two objects.

    If I destroy object 2 then object 1 is still registering the collision between itself and object 2.

    I don't understand why, any help appreciated. Thanks.
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    When are you checking that they are still colliding? When you call Destroy it doesn't get destroyed immediately. It will be destroyed before the next frame, not during the execution of the current frame.
     
  3. jzzja

    jzzja

    Joined:
    Sep 28, 2017
    Posts:
    43
    Please see my example code, the problem occurs indefinitely and not for just a few frames.
    To test that this wasn't due to some other part of my code I made a new test project and added 2 objects as described in my original post. I have rigid bodies and colliders on both objects.

    object 1

    Code (CSharp):
    1.     private bool _player_platform_collision;
    2.  
    3.     void OnCollisionEnter (Collision _collision) {
    4.  
    5.         if (_collision.gameObject.tag=="floor") {
    6.         _player_platform_collision = true;
    7.         }
    8.  
    9.     }
    10.  
    11.     void OnCollisionStay (Collision _collision) {
    12.  
    13.         if (_collision.gameObject.tag=="floor") {
    14.         _player_platform_collision = true;
    15.         }
    16.  
    17.     }
    18.  
    19.     void OnCollisionExit (Collision _collision) {
    20.  
    21.         if (_collision.gameObject.tag=="floor") {
    22.         _player_platform_collision = false;
    23.         }
    24.  
    25.     }
    26.  
    27.     private GUIStyle guiStyle = new GUIStyle();
    28.  
    29.     void OnGUI () {
    30.  
    31.     guiStyle.fontSize = 20;
    32.     GUI.Label (new Rect (0, 0, 100, 40), ": " + _player_platform_collision);
    33.  
    34.     }
    object 2

    Code (CSharp):
    1. void Update() {
    2.  
    3.     if ( Input.GetKey(KeyCode.Space)) {
    4.     Destroy (this.gameObject);
    5.     }
    6.      
    7. }
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Add debugging to OnCollisionStay to see if it is actually still colliding, which I suspect it is not. I think you are expecting OnCollisionExit to be called, but I doubt it works that way, because how can you send a reference in the Collision object for the destroyed gameobject and collider component? I am guessing that OnCollisionStay just stops being called, leaving _player_platform_collision set to true.
     
    jzzja likes this.
  5. jzzja

    jzzja

    Joined:
    Sep 28, 2017
    Posts:
    43
    Thanks that makes sense as the object is destroyed the collision check can't be made, I didn't think of that.