Author Topic: Destroying GameObjects immediately is not permitted during physics trigger/conta  (Read 10247 times)

sfj

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 14
    • View Profile
Hi Guys,
    I'm getting this "Destroying GameObjects immediately is not permitted during physics trigger/contact or animation event callbacks...." error and haven't been able to solve it, apprently it is to do with destroying drawcalls rather than actual physics stuff...the error happens when I'm running in the editor and results in "burnt in" widgets on screen.

Player log:

! about ot do destroy immediate on : _UIDrawCall [joyMaterial]
! (error) Destroying GameObjects immediately is not permitted during physics trigger/contact or animation event callbacks. You must use Destroy instead.

What I'm doing is disabling a panel with widgests and stuff...

Here is my code:

  1.         void OnDisable()
  2.         {
  3.                                
  4.                 //Do stuff with the Panels
  5.                 foreach (GameObject _gameObject in GUIPanelGameObjects)
  6.                 {              
  7.                         if (_gameObject!=null)
  8.                         {                                                              
  9.                                 //Instead of setActiverrecusively
  10.                                 NGUITools.SetActive(_gameObject, false);
  11.                                
  12.                                 Debug.Log("Disabling panel: " + _gameObject.name);
  13.                                
  14.                                                                
  15.                         }
  16.                 }
  17.         }

What I have is a ScreenObject that represents a screen and it's required Panel(s) ( InGameWidgetsPanel,PausePanel...etc).

I use these "ScreenObjects" to control what I see on Screen by disabling/Enabling them and through that also disabling/hiding referenced ngui panels and their widgets.

Would anybody have any advice on how to solve this..?

ThanksInAdvance!

Stefan
« Last Edit: December 20, 2012, 03:24:31 AM by sfj »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
You're trying to disable a part of the UI during a physics callback such as OnCollisionEnter or OnTriggerEnter. That's what's giving you this error. You have to delay your call until the next update. You can do that by setting a boolean flag that you will then later check inside your update function. Or -- better yet -- StartCoroutine(YourFunction());

sfj

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 14
    • View Profile
Hi Aren, thanks for your quick reply. Is this then during a "general physics callback" in unity and not specifically related to any of the widgets in the panel that is to be disabled? ( as I tried removing all buttons with no success). 

So I created coroutine where I use the WaitforSeconds to delay things..and by setting it to as long as five I seem to getting rid of the error...although that is quite a long time in game... ???

How would you do a coroutine?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
WaitForEndOfFrame is enough. No need to wait for seconds.

sfj

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 14
    • View Profile
Fantastic it's working now, yes indeed WaitForEndOfFrame is enough...digging deeper in my code I found other things generating the same error just to make things more confusing... and these things seem to have needed a few seconds to clear, thanks for your help!   :)