x


OnTriggerExit isn't called when the object is destroyed?

I have such script:

  private void OnTriggerEnter(Collider other)
  {
    if (triggered == 0)
    {
      Highlight();
    }
    ++trigerred;
  }

  internal void OnTriggerExit(Collider other)
  {
    --triggered;
    if (triggered == 0)
    {
      Dehighlight();
    }
  }

After objects were triggered and another of them was destroyed OnTriggerExit() of current object isn't invoked and object remains highlighted. Why so?

more ▼

asked Feb 28 '11 at 04:17 PM

Anton Grigoryev gravatar image

Anton Grigoryev
4 2 2 5

(comments are locked)
10|3000 characters needed characters left

1 answer: sort newest

Because that's the way it works, whether it's a bug or intentional I don't know, but I think it's always been like that. My solution is to move the object far away for one frame before destroying it; that way OnTriggerExit will always fire.

more ▼

answered Feb 28 '11 at 04:32 PM

Eric5h5 gravatar image

Eric5h5
81.5k 42 133 529

Thanks for your answer. I have solved this problem using list of triggered object and new OnDestroy() method (in Unity 3.2). In OnDestroy() I forcedly call OnTriggerExit() on all with destroyed object triggered objects. In this way it works!

Mar 04 '11 at 09:08 AM Anton Grigoryev
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x49
x18

asked: Feb 28 '11 at 04:17 PM

Seen: 1435 times

Last Updated: Feb 28 '11 at 04:17 PM