Search Unity

Resolved Destroying GameObjects immediately is not permitted during physics trigger/contact, animation event

Discussion in 'Scripting' started by Daviiid, Sep 23, 2022.

  1. Daviiid

    Daviiid

    Joined:
    Oct 9, 2015
    Posts:
    50
    After collision of 2 game objects(collisionStay) I run this script:
    Code (CSharp):
    1. public void TakeDamage(int damage)
    2.     {
    3.         startHP -= damage;
    4.         if(startHP <= 0)
    5.         {
    6.             float x = transform.position.x, y = transform.position.y, z = transform.position.z;
    7.             var pos = walls.WorldToCell(transform.position);
    8.             walls.SetTile(pos, null);
    9.             GraphNode node = AstarPath.active.GetNearest(new Vector3(x, y + 1f, z)).node;
    10.             node.Penalty = 1000;
    11.             node = AstarPath.active.GetNearest(new Vector3(x, y + .5f, z)).node;
    12.             node.Penalty = 1000;
    13.             node = AstarPath.active.GetNearest(new Vector3(x - .5f, y + 1f, z)).node;
    14.             node.Penalty = 1000;
    15.             node = AstarPath.active.GetNearest(new Vector3(x - .5f, y + .5f, z)).node;
    16.             node.Penalty = 1000;          
    17.             gameObject.GetComponent<BoxCollider2D>().enabled = false;
    18.             Destroy(gameObject,.1f);
    19.         }
    20.     }
    And I get this error:
    Code (CSharp):
    1. Destroying GameObjects immediately is not permitted during physics trigger/contact, animation event callbacks, rendering callbacks or OnValidate. You must use Destroy instead.
    2.  
    3. 0x00007ff69e2a363d (Unity) StackWalker::GetCurrentCallstack
    4. 0x00007ff69e2aa3b9 (Unity) StackWalker::ShowCallstack
    5. 0x00007ff69f24bd03 (Unity) GetStacktrace
    6. 0x00007ff69f8f9633 (Unity) DebugStringToFile
    7. 0x00007ff69de59363 (Unity) CanDestroyObject
    8. 0x00007ff69de5ca25 (Unity) DestroyObjectHighLevel
    9. 0x00007ff69e930edf (Unity) Tilemap::CheckAndDestroyTileInstantiatedObject
    10. 0x00007ff69e91ab6f (Unity) Tilemap::ClearTile<0>
    11. 0x00007ff69e940007 (Unity) Tilemap::SetTileAsset
    12. 0x00007ff69d61ecd8 (Unity) Tilemap_CUSTOM_SetTileAsset_Injected
    13. 0x000002b2b04e538f (Mono JIT Code) (wrapper managed-to-native) UnityEngine.Tilemaps.Tilemap:SetTileAsset_Injected (UnityEngine.Tilemaps.Tilemap,UnityEngine.Vector3Int&,UnityEngine.Object)
    14. 0x000002b2b04e52ab (Mono JIT Code) UnityEngine.Tilemaps.Tilemap:SetTileAsset (UnityEngine.Vector3Int,UnityEngine.Object)
    15. 0x000002b2b04e5233 (Mono JIT Code) UnityEngine.Tilemaps.Tilemap:SetTile (UnityEngine.Vector3Int,UnityEngine.Tilemaps.TileBase)
    16. 0x000002b2b04ec4b3 (Mono JIT Code) [HP.cs:24] HP:TakeDamage (int)
    17. 0x000002b2b04e9d5b (Mono JIT Code) [GreenSlimeController.cs:244] GreenSlimeController:Collide (UnityEngine.Collision2D)
    18. 0x000002b2b04e9dd3 (Mono JIT Code) [GreenSlimeController.cs:222] GreenSlimeController:OnCollisionStay2D (UnityEngine.Collision2D)
    19. 0x000002b2aa85eb63 (Mono JIT Code) (wrapper runtime-invoke) <Module>:runtime_invoke_void__this___object (object,intptr,intptr,intptr)
    20. 0x00007ffcca5e02f4 (mono-2.0-bdwgc) [mini-runtime.c:3445] mono_jit_runtime_invoke
    21. 0x00007ffcca51eb34 (mono-2.0-bdwgc) [object.c:3066] do_runtime_invoke
    22. 0x00007ffcca51eccc (mono-2.0-bdwgc) [object.c:3113] mono_runtime_invoke
    23. 0x00007ff69e1c80f4 (Unity) scripting_method_invoke
    24. 0x00007ff69e1c2bf4 (Unity) ScriptingInvocation::Invoke
    25. 0x00007ff69e194ffb (Unity) MonoBehaviour::HandleNotifications
    26. 0x00007ff69db47f26 (Unity) GameObject::SendMessageAny
    27. 0x00007ff69e8acaeb (Unity) PhysicsContacts2D::SendCallbackReports
    28. 0x00007ff69e8aae71 (Unity) PhysicsContacts2D::ProcessContacts
    29. 0x00007ff69e87acc8 (Unity) PhysicsManager2D::Simulate
    30. 0x00007ff69e8772ef (Unity) PhysicsManager2D::FixedUpdate
    31. 0x00007ff69e877436 (Unity) `PhysicsManager2D::Initialize'::`2'::FixedUpdatePhysics2DFixedUpdateRegistrator::Forward
    32. 0x00007ff69de730dc (Unity) ExecutePlayerLoop
    33. 0x00007ff69de731b3 (Unity) ExecutePlayerLoop
    34. 0x00007ff69de78f70 (Unity) PlayerLoop
    35. 0x00007ff69edeebbe (Unity) PlayerLoopController::UpdateScene
    36. 0x00007ff69edecda2 (Unity) Application::TickTimer
    37. 0x00007ff69f25195a (Unity) MainMessageLoop
    38. 0x00007ff69f256764 (Unity) WinMain
    39. 0x00007ff6a05a9aee (Unity) __scrt_common_main_seh
    40. 0x00007ffd4ddf54e0 (KERNEL32) BaseThreadInitThunk
    41. 0x00007ffd4eec485b (ntdll) RtlUserThreadStart
    I don't know why. And I never had any issues destroying game objects before.
    After googling for a while I've found DestroyImmidiate which I didn't even know existed before.
    Also if those 2 gameobjects are staying in contact when am I'm supossed to destroy it?
    I've tried setting the collider.enable to false and executing the destroy .1f later, but I'm still getting that error.
     
  2. Franco_Voisard

    Franco_Voisard

    Joined:
    Apr 30, 2018
    Posts:
    20
    It seems to be an issue with the TileMap method. The SetTile method is calling an internal "ClearTile" That is using the "DestroyImmediate" method.
     
  3. Daviiid

    Daviiid

    Joined:
    Oct 9, 2015
    Posts:
    50
    Thanks that helped me. And I've fixed it. It seems like you can't just setTile to null on collide.
    For future generations here is the fix:
    First I've made the collider disabled and set the sprite to null so it appears as if it was destroyed. I've deleted the line Destroy(gameObject). Then I've started a coroutine to delay the setTile(null) which deleted the gameobject as well since it was attached on the rule tile.
     
  4. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    Sorry about this! This looks like a bug and we will check it out, thanks!
     
    Luis0413, Untherow, Daviiid and 2 others like this.
  5. muskrat_DYG

    muskrat_DYG

    Joined:
    Sep 16, 2021
    Posts:
    1
    Hello @ChuanXin, any updates on the progress of this? I am facing a similar issue.
     
  6. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    Hi, sorry for the late reply! This bug should have been fixed. For the 2021.3 LTS, it has been fixed on 2021.3.15f1.
     
    Daviiid likes this.