Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: theHost on April 16, 2012, 05:25:17 AM

Title: DestroyImmediate during collision callback
Post by: theHost on April 16, 2012, 05:25:17 AM
Just got this Unity error when one of my enemy units with an HP bar hit the killzone (collider) in the level. Basically the enemy unit hit the killzone, which killed the enemy unit, and in turn the HUD turned off the HP bar for that unit by calling NGUITools.SetActive(). Is there a better way to handle it? Move it off screen?

Quote
Destroying object immediately is not permitted during physics trigger and contact callbacks. You must use Destroy instead.
UnityEngine.Object:DestroyImmediate(Object)
NGUITools:DestroyImmediate(Object) (at Assets/NGUI/Scripts/Internal/NGUITools.cs:441)
UIPanel:OnDisable() (at Assets/NGUI/Scripts/UI/UIPanel.cs:519)
UnityEngine.GameObject:set_active(Boolean)
NGUITools:Deactivate(Transform) (at Assets/NGUI/Scripts/Internal/NGUITools.cs:508)
NGUITools:SetActive(GameObject, Boolean) (at Assets/NGUI/Scripts/Internal/NGUITools.cs:523)

Title: Re: DestroyImmediate during collision callback
Post by: ArenMook on April 16, 2012, 09:32:55 AM
Oh that's interesting. Not permitted during physics callbacks? Curious. Thing is... that's draw calls getting destroyed (ie: generated UI geometry), and have nothing to do with physics.

Unfortunately that particular piece of code has to stay at 'immediate' or the UI won't be hidden properly when exiting play mode, or something else along those lines (I had a lot of headaches trying to make it work properly back in December).

My suggestion... delay the destruction of your HP bar:
  1. UpdateManager.AddDestroy(uiGameObject, 0.001f);
...or delay the turning it off:
  1. UpdateManager.AddUpdate(...);
Title: Re: DestroyImmediate during collision callback
Post by: theHost on April 16, 2012, 06:28:40 PM
Ya I can delay it.