1. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice
  2. We're looking for feedback on Unity Starter Kits! Let us know what you’d like.
    Dismiss Notice
  3. We’re giving 2017.1 beta testers a chance to win t-shirts and a Nintendo Switch. Read more on the blog.
    Dismiss Notice
  4. We want to know how you learned Unity! Help us by taking this quick survey and have a chance at a $25 gift card
    Dismiss Notice
  5. Unity 5.6 is now released.
    Dismiss Notice
  6. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice

OnCollisionExit2D is not getting called when object is destroyed during collision ??

Discussion in '2D' started by faris_arifiansyah, Jan 30, 2015.

  1. faris_arifiansyah

    faris_arifiansyah

    Joined:
    Jan 30, 2015
    Posts:
    3
    first of all, I'm sorry for my bad english..

    I want to create my first game and I'm totally newbie in unity.

    is it true that OnCollisionExit2D method will not being called if the object is destroyed??

    I have a fortress/wall in my game. The wall has a HealthPoint.
    That wall is going to be destroyed by the enemy.

    So, I'm using OnCollisionStay2D method in the enemy script that is used to damage the fortress/wall while the enemy and the fortress is colliding.

    When the HP is 0, the wall will be destroyed. I think when this happen, the OnCollisionExit2D method in the enemy script will be called. But in fact, that method is not getting called..

    I want to change the animation of the enemy when it enter a collision (with fortress) and after the wall has destroyed.

    This is the script (enter collision):
    Code (CSharp):
    1. void OnCollisionEnter2D(Collision2D collision)
    2.     {
    3.        
    4.         FortressScript wall = collision.gameObject.GetComponent<FortressScript> ();
    5.  
    6.         if (wall != null)
    7.         {
    8.             this.ChangeAnimationState(1);
    9.             this.attackStats = true;
    10.         }
    11.     }
    In Collision:
    Code (CSharp):
    1. void OnCollisionStay2D(Collision2D collision)
    2.     {
    3.         FortressScript fortress = collision.gameObject.GetComponent<FortressScript> ();
    4.  
    5.         if (fortress != null)
    6.         {
    7.             HealthScript fortressHP = fortress.GetComponent<HealthScript>();
    8.            
    9.             if(fortressHP != null){
    10.                 fortressHP.Damage(0.1f);
    11.             }
    12.  
    13.  
    14.         }
    15.     }
    Exit Collision:
    Code (CSharp):
    1. void OnCollisionExit2D(Collision2D coll)
    2.     {
    3.         //this method is not getting called
    4.         this.ChangeAnimationState (2);
    5.  
    6.     }

    Is there a solution?? Thanks :D
     
  2. CodeMonke234

    CodeMonke234

    Joined:
    Oct 13, 2010
    Posts:
    181
  3. faris_arifiansyah

    faris_arifiansyah

    Joined:
    Jan 30, 2015
    Posts:
    3
  4. CodeMonke234

    CodeMonke234

    Joined:
    Oct 13, 2010
    Posts:
    181
    Sorry - I think that link says exactly what you need:
    OnCollisionExit() will only fire when the object stops colliding with an object, so that is the intended use. Code can't execute on an object that is destroyed (because the code was destroyed with it). .....

    I believe easy way to solve this it to have the part that calls object.destroy() also execute the code you want to execute on OnCollisionExit().
     
  5. faris_arifiansyah

    faris_arifiansyah

    Joined:
    Jan 30, 2015
    Posts:
    3
    I think object.destroy() method can't take a parameter.
    I need to know what object does the fortress/wall collide with.. So, I think this can't be done by using the destroy() method..

    But, finally I've found the solution..
    I manage to move the object first (so the OnCollisionExit is fired).. and then using start coroutine to waitForFixedUpdate() and finally destroy the object.

    By the way, thank you for your suggestion.. I really appreciate you :D

    FortressScript:
    Code (CSharp):
    1. void Update()
    2.     {
    3.         if(this.HP() <= 0)
    4.         {
    5.             this.gameObject.transform.Translate(Vector3.left * 9999);
    6.             StartCoroutine("SelfDestroy");
    7.         }
    8.     }
    9.  
    10.     IEnumerator SelfDestroy(){
    11.         yield return new WaitForFixedUpdate();
    12.         Destroy (this.gameObject);
    13.     }