Search Unity

Better way to initiate onTriggerExit2D() [solved]

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

  1. lothop

    lothop

    Joined:
    Feb 21, 2014
    Posts:
    22
    Is there a better way to initiate onTriggerExit2D() than to move the unit away, then destroy it after a time delay?
    I found that the time delay was needed before destroying otherwise the onTriggerExit2D() would not work.

    Code (csharp):
    1.  
    2. void Update()
    3. {
    4. // If the player has zero or less health...
    5. if (damageToUnit >= currentHealth) {
    6.     Debug.Log ("dead");
    7.     transform.position = new Vector3 (100, 100);
    8.     Invoke("destroyUnit", 0.01f);
    9.  
    10.  }
    11. }
    12.  
    13. void destroyUnit()
    14. {
    15.     Destroy (gameObject);
    16. }
    Thanks for reading.
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You can always call OnTriggerXXX directly.
     
  3. lothop

    lothop

    Joined:
    Feb 21, 2014
    Posts:
    22
    Only issue with this is that it will also kill the unit that is the victor.
    After the unit is moved away both units will OnTriggerXXX.

    If I test currenthealth before killing it would solve it. Which way is best?