Author Topic: "Destroying GameObjects immediately is not permitted" - But i don´t destroy!  (Read 10811 times)

Chaosgod_Espér

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
Hi there..

I want to SetActive a UI GameObject at Runtime. The Lines for that action are:
  1.         void OnTriggerEnter(){
  2.                 if(InteractSymbol != null){
  3.                         NGUITools.SetActive(InteractSymbol, true);
  4.                         InteractSymbol.transform.FindChild("Label").GetComponent<UILabel>().text = cInput.GetText("Interact");
  5.                 }
  6.         }
  7.         void OnTriggerExit(){
  8.                 if(InteractSymbol != null){
  9.                         NGUITools.SetActive(InteractSymbol, false);
  10.                 }
  11.         }

Now on a Testplay, Unity gives me the following Error:
[spoiler]
  1. Destroying GameObjects immediately is not permitted during physics trigger/contact, animation event callbacks or OnValidate. You must use Destroy instead.
  2. UnityEngine.Object:DestroyImmediate(Object)
  3. NGUITools:DestroyImmediate(Object) (at Assets/NGUI/Scripts/Internal/NGUITools.cs:1085)
  4. UIDrawCall:ReleaseInactive() (at Assets/NGUI/Scripts/Internal/UIDrawCall.cs:839)
  5. UIDrawCall:ReleaseAll() (at Assets/NGUI/Scripts/Internal/UIDrawCall.cs:827)
  6. UIPanel:OnDisable() (at Assets/NGUI/Scripts/UI/UIPanel.cs:1014)
  7. UnityEngine.GameObject:SetActive(Boolean)
  8. NGUITools:SetActiveSelf(GameObject, Boolean) (at Assets/NGUI/Scripts/Internal/NGUITools.cs:1272)
[/spoiler]

The problem, as you can see at the Lines, is: I don´t destroy any object. I instantiated and Destroyed the Object, before i changed to SetActive - yes.. But i Changed it, saved it several times now.. But NGUI still thinks i´m destroying the Objects ._.

What´s wrong here?

pretender

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 155
    • View Profile
Don't destroy/disable objects in OnTriggerEnter/OnCollisionEnter, make a coroutine to destroy/disable them instead...I believe simple method will work too.

Chaosgod_Espér

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
As said - i DON´T Destroy.. i simply change SetActive

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
SetActive on panels makes the panel destroy the draw call. You still need to delay it until outside the physics call.

Try to do this:

  1. void OnTriggerEnter()
  2. {
  3.         if(InteractSymbol != null)
  4.         {
  5.                 StartCoroutine(DoDelayed(() => {
  6.                         NGUITools.SetActive(InteractSymbol, true);
  7.                         InteractSymbol.transform.FindChild("Label").GetComponent<UILabel>().text = cInput.GetText("Interact");                 
  8.                 }));
  9.         }
  10. }
  11. void OnTriggerExit()
  12. {
  13.         if(InteractSymbol != null)
  14.         {
  15.                 StartCoroutine(DoDelayed(() => {
  16.                         NGUITools.SetActive(InteractSymbol, false);
  17.                 }));
  18.         }
  19. }      
  20. IEnumerator DoDelayed(System.Action action)
  21. {
  22.         yield return null;
  23.         if (action != null) action();
  24. }
  25.  

Disastercake

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 87
  • www.disastercake.com
    • View Profile
    • Disastercake
Does NGUITools.SetActive have much added functionality now that Unity automatically disables objects under the parent?

It might be cool for NGUITools.SetActive to get some added functionality by doing this sort of IEnumerator yield for you, maybe by passing in an extra bool value to clarify you want it to wait a frame.
Creator of Soul Saga.
http://www.disastercake.com

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
NGUITools.SetActive is there because it was added long before Unity 4, so it's there for legacy purposes more than anything else.