Search Unity

[SOLVED] (C#) Calling OnTriggerExit when collider destroyed.

Discussion in 'Physics' started by Gunging, Feb 1, 2018.

  1. Gunging

    Gunging

    Joined:
    Sep 6, 2016
    Posts:
    139
    Hello everyone, I am being troubled by a logic problem here:
    When a collider is inside a trigger, and the trigger is destroyed or disabled, OnTriggerExit() is not called, however I remember this used to cause it to run so the code that once worked is broken.

    The thing that makes this annoying, is that the trigger is a gravity aura of a blackhole, and when the player's spaceship is in range, the player script calls OnTriggerEnter with the following code:

    Code (CSharp):
    1.     void OnTriggerEnter(Collider coll) {
    2.         if (coll.attachedRigidbody.gameObject.name == "Gravity Aura") { Warn("Caught in a Gravity Aura!", 2); }
    3.     }
    And well, whenever the player escapes by just going far enough from the blackhole, OnTriggerExit is called correctly:

    Code (CSharp):
    1.     void OnTriggerExit(Collider coll) {
    2.         if (coll.attachedRigidbody.gameObject.name == "Gravity Aura" && warning == 2) { Warn("", 0); }
    3.     }
    But, when the blackhole is destroyed, OnTriggerExit is not called, so the pop-up window that tells the player they are caught in a gravity aura never goes away.

    Code (CSharp):
    1.     void Warn(string reason, int warningId) {
    2.         if (reason == "") { //Disables all pop-up window elements
    3.             floatingWarning.enabled = false;
    4.             floatingWarningText.enabled = false;
    5.             holoWarning.enabled = false;
    6.             holoWarningText.enabled = false;
    7.             warning = 0;
    8.         } else { //Enables all pop-up window elements, states the reason.
    9.              floatingWarning.enabled = true;
    10.              floatingWarningText.enabled = true;
    11.              floatingWarningText.text = reason;
    12.              holoWarning.enabled = true;
    13.              holoWarningText.enabled = true;
    14.              holoWarningText.text = reason;
    15.              warning = warningId;
    16.            
    17.         }
    18.     }

    If you have a suggestion to call the void Warn() with simpler code, please share it :)

    Thanks in advance
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I remember hearing about this "issue", I think.

    I think you could track if the user was inside it ( a script on the blackhole) & on it's disable you could send a notification to the player (if they're inside). This could be a list if more than the player is susceptible. I suppose you could do this on the player, too, just might be easier on the specific object.

    I did a quick search and saw a few people say that you could also move the object in question before destroying it, thus forcing the exit to occur before it's disabled/destroyed.
     
    Stexe and Gunging like this.
  3. Gunging

    Gunging

    Joined:
    Sep 6, 2016
    Posts:
    139
    For the record, moving the collider to someplace far away works only if the collider is enabled during the moment the displacing takes happens.

    Thanks