Search Unity

OnCollisionExit trouble

Discussion in 'Scripting' started by Scope, May 27, 2011.

  1. Scope

    Scope

    Joined:
    Mar 20, 2010
    Posts:
    95
    For some reason, OnCollisionExit dosn't seem to be working, is it just my code? OnCollisionEnter works fine, but unity dosn't seem to realize that the collision has ended.

    Here's my code:

    Code (csharp):
    1. function OnCollisionEnter(theCollision : Collision)
    2. {
    3.     if (theCollision.gameObject.tag == "EvilOne")
    4.     {
    5.         timer = true;
    6.         moving = false;
    7.     }
    8. }
    9.  
    Thats for detecting the collision, then stopping the object from moving and starting a timer (which are both done in the update function and both work fine)
    Code (csharp):
    1. function OnCollisionExit(theCollision : Collision)
    2. {
    3.     if(theCollision.gameObject.tag == "EvilOne")
    4.     {
    5.         timer = false;
    6.         moving = true;
    7.     }
    8. }
    and this is for detecting the end of the collision in the same script to stop the timer, and start moving again, but these things don't happen. I don't see any flaws in the script, could it be a unity bug?
     
  2. Scope

    Scope

    Joined:
    Mar 20, 2010
    Posts:
    95
    I have just figured out that if the object it is colliding with moves away from it, OnCollisionExit fires, but if the object is dsestroyed (which was what I was making it do) OnCollisionExit dosn't fire. Anyone know why? or how to work around this?
     
  3. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    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). What exactly are you trying to do?
     
  4. RabidCicada

    RabidCicada

    Joined:
    May 24, 2011
    Posts:
    17
    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().

    So put common code in a function:
    Code (csharp):
    1.  
    2. releaseObject(){
    3.   timer = false;
    4.   moving = true;
    5. }
    Then in OnCollisionExit()
    Code (csharp):
    1.  
    2. //dostuff
    3. releaseObject();
    4.  
    In function that calls object.Destroy():
    Code (csharp):
    1.  
    2. releaseObject();
    3. //Do destruction stuff
    4.  
    You might need to use otherObject.releaseObject() or something like that.
     
    Last edited: May 27, 2011
  5. Scope

    Scope

    Joined:
    Mar 20, 2010
    Posts:
    95
    I don't mean that the object with the script is destroyed, its the object that its colliding with. When the other object is destroyed, OnCollisionExit doesn't fire. I'm trying to make two objects (two diffrent units of opposing teams) "fight" each other. They collide, stop moving, and timers start for each of them, they take damage based on the timer, then when one of them "dies" that object is destroyed. then when the object that "won" the fight, continues moving, and the timer stops. All of it works exept after one unit dies, the other dosn't move, and the timer continues running. This I have figured out is due to OnCollisionExit not firing :/
     
  6. Scope

    Scope

    Joined:
    Mar 20, 2010
    Posts:
    95
    I've figured out that it only dosn't fire when the object its colliding with is destroyed, but it does if the object its colliding with is moved to a diffrent position. Anyone know why?
     
  7. RabidCicada

    RabidCicada

    Joined:
    May 24, 2011
    Posts:
    17
    I understand your frustration...but it doesn't really matter. It might have been a philisophical design decision "it never stopped colliding...it just left existence while still colliding". And you can argue that clearly it stopped colliding if it no longer exists.

    Maybe there's a bug submission prcess you could submit this in. Ultimately its probably easy to work around so don't waste time waiting on an answer:)
     
  8. Scope

    Scope

    Joined:
    Mar 20, 2010
    Posts:
    95
    yeah, your right, it would just be way easier if I could simply destroy the object.
    Thanks for your help, it is much appreciated! :)