Search Unity

OnTriggerExit2D doesn't work

Discussion in '2D' started by Vifi, Dec 16, 2014.

  1. Vifi

    Vifi

    Joined:
    Aug 5, 2013
    Posts:
    28
    Hi
    In project i am working at i have circle2d trigger around enemy. This trigger slowing bullets inside.
    The thing is, if somehow you will kill enemy with trigger, function OnTriggerExit2d is not called.
    I already tried to move my enemy 1000 unitys on x and y before destroy but it also didn't work.
    Is there any way to call it manualy?
     
  2. Ilingis

    Ilingis

    Joined:
    Dec 13, 2012
    Posts:
    63
    In order for OnTrigger2D to work one object needs to have a Rigidbody2D.
    Can you post your code?
     
  3. Vifi

    Vifi

    Joined:
    Aug 5, 2013
    Posts:
    28
    I don't have problem with trigger working. OnTriggerEnter2D is working fine. Just when object with trigger collider is destroyed, OnTriggerExit2D is not called.

    Here is my code:
    Code (CSharp):
    1. float speed = 3f;
    2.     // Use this for initialization
    3.     void Start () {
    4.    
    5.     }
    6.    
    7.     // Update is called once per frame
    8.     void Update () {
    9.    
    10.     }
    11.     void OnTriggerEnter2D (Collider2D col)
    12.     {
    13.         if (col.tag == "KarabinPocisk")
    14.             col.GetComponent<KarabinPocisk> ().GetSpeed /= speed;
    15.         if (col.tag == "RakietaPocisk")
    16.             col.GetComponent<RakietaPocisk> ().GetSpeed /= speed;
    17.         if (col.tag == "FalaPocisk")
    18.             col.GetComponent<FalaPocisk> ().GetSpeed /= speed;
    19.  
    20.     }
    21.  
    22.     void OnTriggerExit2D (Collider2D col)
    23.     {
    24.         if (col.tag == "KarabinPocisk")
    25.                         col.GetComponent<KarabinPocisk> ().GetSpeed *= speed;
    26.         if (col.tag == "RakietaPocisk")
    27.                     col.GetComponent<RakietaPocisk> ().GetSpeed *= speed;
    28.         if (col.tag == "FalaPocisk")
    29.             col.GetComponent<FalaPocisk> ().GetSpeed *= speed;
    30.     }
     
  4. Ilingis

    Ilingis

    Joined:
    Dec 13, 2012
    Posts:
    63
    Have you tried it with OnTriggerStay2D?
     
  5. Vifi

    Vifi

    Joined:
    Aug 5, 2013
    Posts:
    28
    I can't do it in Stay because it is function called every frame. So if i will divide or whatever it will get near 0 after about 2-3 seconds, anyway OnTriggerExit2D still would not work...
     
  6. Xelnath

    Xelnath

    Joined:
    Jan 31, 2015
    Posts:
    402
    I ran into this issue as well. I had to store a list of objects that were inside the trigger, then loop over them when the trigger is destroyed.

    onTriggerExit2D doesn't get called when the trigger is destroyed. (Not sure why or why that isn't an option.) Could be a legit tech issue.
     
  7. Xelnath

    Xelnath

    Joined:
    Jan 31, 2015
    Posts:
    402
    A code example:

    Code (csharp):
    1.  
    2. Somewhere in OnTriggerEnter2D...
    3.             AttackableInsiders.Add(attackable);
    4.  
    Then later

    Code (csharp):
    1.  
    2.     void OnDestroy()
    3.     {
    4.         foreach ( AttackableSpot attackable in AttackableInsiders )
    5.         {
    6.             if ( attackable != null && attackable.isActiveAndEnabled )
    7.             {
    8.                 IUnit source = GetUnit ();
    9.                 IUnit target = attackable.GetUnit ();
    10.                 if ( target == null )
    11.                     Debug.LogError ( "Detected an attackable spot without a parent." );
    12.  
    13.                 // Bundle the attack message and figure out how to handle it
    14.                 using (var c = Context.Get())
    15.                 {
    16.                     c.Units ["Source"] = source;
    17.                     c.Units ["Target"] = target;
    18.                     c ["Attackable"] = true;
    19.                     c ["Blocking"] = false;
    20.                     c ["Missile"] = false;
    21.                     c ["Name"] = name;
    22.                     source.OnSignal ( sExitSignal, c );
    23.                 }
    24.             }
    25.         }
    26.         AttackableInsiders.Clear();
    27.     }
    28.  
     
  8. inthemilkywat

    inthemilkywat

    Joined:
    Dec 29, 2013
    Posts:
    56
    Maybe you can put the trigger as the parent object and enemy as a child object. Then when the enemy is killed, you destroy the child object and you will still be able to use onTriggerExit2D from the parent object.