Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We are updating our Terms of Service for all Unity subscription plans, effective October 13, 2022, to create a more streamlined, user-friendly set of terms. Please review them here: unity.com/legal/terms-of-service.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice
  4. Join us on Thursday, September 29, for a day with Unity's SRP teams here on the forum or on Reddit, and discuss topics around URP, HDRP, and the Scriptable Render Pipeline in general.
    Dismiss Notice

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

Discussion in 'Scripting' started by Daviiid, Sep 23, 2022 at 7:25 PM.

  1. Daviiid

    Daviiid

    Joined:
    Oct 9, 2015
    Posts:
    32
    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:
    32
    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.
     
    Franco_Voisard likes this.
  4. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    964
    Sorry about this! This looks like a bug and we will check it out, thanks!
     
    Daviiid, Bunny83 and Franco_Voisard like this.
unityunity