Search Unity

OnTriggerExit2D - Multiple triggers Exiting

Discussion in 'Scripting' started by lothop, Feb 28, 2015.

  1. lothop

    lothop

    Joined:
    Feb 21, 2014
    Posts:
    22
    The box moving up is connecting with multiple boxes and taking their combined damage.
    As each box hits the one moving up their damage gets added to the total damage its taking.

    As the boxes are destroyed I want their damage to be subtracted from the damage its taking.

    Code (csharp):
    1. void OnTriggerExit2D(Collider2D coll){
    2.         if (damageToUnit >= currentHealth) {
    3.             enemy = coll.gameObject;
    4.  
    5.             initiateCombat enemyCombat = enemy.GetComponent<initiateCombat> ();
    6.             enemyCombat.dealDamage -= unitStats.damage;
    7.  
    8.             Destroy (gameObject);
    9.         }
    10.     }
    11.  
    This works fine as each sideways box is only OnTriggerExit2D'ed on the box moving up. The colliders are clean. In this clip the damage the box moving up takes is 20+0 then 20+20 and when they die, 40 - 20, 20 - 20.


    This doesnt work because OnTriggerExit2D is triggering off the sideways box and not the box moving up. The colliders are all interconnected. In this clip the damage the box moving up takes is 20+0 then 20+20 and when they die, 40 - 0, 40 - 20.


    How do I tell it to only OnTriggerExit2D with the box moving up?
     
  2. lothop

    lothop

    Joined:
    Feb 21, 2014
    Posts:
    22
    Solved the issue.
    Edit -> Project Settings -> Physics 2D and set the "Enemy" layer to not interact with one another