Author Topic: Bug: Editor DestroyImmediate?  (Read 4590 times)

kinetifex

  • Guest
Bug: Editor DestroyImmediate?
« on: June 17, 2013, 03:56:59 PM »
In the Unity Editor, NGUI elements are not being hidden properly and would not be destroyed even between play sessions. I'm getting the following error related to a triggered event call:

Quote
Destroying GameObjects immediately is not permitted during physics trigger/contact or animation event callbacks. You must use Destroy instead.
UnityEngine.Object:DestroyImmediate(Object)
NGUITools:DestroyImmediate(Object) (at Assets/NGUI/Scripts/Internal/NGUITools.cs:514)

Looking at line 514 in NGUITools, if the Application isEditor, then UnityEngine.Object.DestroyImmediate is called.

Changing this to always call:
  1. UnityEngine.Object.Destroy(obj);
seems to fix the error.

Any reason DestroyImmediate was being called? Can this be fixed in the next version?

Thanks!

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Bug: Editor DestroyImmediate?
« Reply #1 on: June 17, 2013, 05:58:00 PM »
Don't destroy remove widgets or panels from animation callbacks or physics triggers. :)

You can delay it by doing this

  1. //where you would normally trigger your removal:
  2. StartCoroutine(MyDestroyerMethod);
  3.  
  4.  
  5. IEnumerator MyDestryerMethod()
  6. {
  7. yield return null;
  8. //Do the disable call that triggers the problem.
  9. }
  10.  

The DestroyImmediate is used because it will cause problems in the Editor otherwise, as far as I know. You'll have to ask ArenMook about that.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Bug: Editor DestroyImmediate?
« Reply #2 on: June 17, 2013, 09:23:09 PM »
Yeah, UnityEngine.Object.Destroy(obj); doesn't work in the editor.

kinetifex

  • Guest
Re: Bug: Editor DestroyImmediate?
« Reply #3 on: June 18, 2013, 01:59:35 PM »
Ok, switching to UnityEngine.Object.Destroy(obj); seemed to fix this issue fine for me in the Editor, so I'm not sure why you wouldn't use that?

However, delaying with a co-routine is working, too so I'll just run with that.

Thanks.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Bug: Editor DestroyImmediate?
« Reply #4 on: June 18, 2013, 02:08:53 PM »
Unhide the hidden game objects by selecting "geometry" debugging on the panels. You will notice that using UnityEngine.Object.Destroy they never go away after play/stop -- they just keep piling up.