After collision of 2 game objects(collisionStay) I run this script:
public void TakeDamage(int damage)
{
startHP -= damage;
if(startHP <= 0)
{
float x = transform.position.x, y = transform.position.y, z = transform.position.z;
var pos = walls.WorldToCell(transform.position);
walls.SetTile(pos, null);
GraphNode node = AstarPath.active.GetNearest(new Vector3(x, y + 1f, z)).node;
node.Penalty = 1000;
node = AstarPath.active.GetNearest(new Vector3(x, y + .5f, z)).node;
node.Penalty = 1000;
node = AstarPath.active.GetNearest(new Vector3(x - .5f, y + 1f, z)).node;
node.Penalty = 1000;
node = AstarPath.active.GetNearest(new Vector3(x - .5f, y + .5f, z)).node;
node.Penalty = 1000;
gameObject.GetComponent<BoxCollider2D>().enabled = false;
Destroy(gameObject,.1f);
}
}
And I get this error:
Destroying GameObjects immediately is not permitted during physics trigger/contact, animation event callbacks, rendering callbacks or OnValidate. You must use Destroy instead.
0x00007ff69e2a363d (Unity) StackWalker::GetCurrentCallstack
0x00007ff69e2aa3b9 (Unity) StackWalker::ShowCallstack
0x00007ff69f24bd03 (Unity) GetStacktrace
0x00007ff69f8f9633 (Unity) DebugStringToFile
0x00007ff69de59363 (Unity) CanDestroyObject
0x00007ff69de5ca25 (Unity) DestroyObjectHighLevel
0x00007ff69e930edf (Unity) Tilemap::CheckAndDestroyTileInstantiatedObject
0x00007ff69e91ab6f (Unity) Tilemap::ClearTile<0>
0x00007ff69e940007 (Unity) Tilemap::SetTileAsset
0x00007ff69d61ecd8 (Unity) Tilemap_CUSTOM_SetTileAsset_Injected
0x000002b2b04e538f (Mono JIT Code) (wrapper managed-to-native) UnityEngine.Tilemaps.Tilemap:SetTileAsset_Injected (UnityEngine.Tilemaps.Tilemap,UnityEngine.Vector3Int&,UnityEngine.Object)
0x000002b2b04e52ab (Mono JIT Code) UnityEngine.Tilemaps.Tilemap:SetTileAsset (UnityEngine.Vector3Int,UnityEngine.Object)
0x000002b2b04e5233 (Mono JIT Code) UnityEngine.Tilemaps.Tilemap:SetTile (UnityEngine.Vector3Int,UnityEngine.Tilemaps.TileBase)
0x000002b2b04ec4b3 (Mono JIT Code) [HP.cs:24] HP:TakeDamage (int)
0x000002b2b04e9d5b (Mono JIT Code) [GreenSlimeController.cs:244] GreenSlimeController:Collide (UnityEngine.Collision2D)
0x000002b2b04e9dd3 (Mono JIT Code) [GreenSlimeController.cs:222] GreenSlimeController:OnCollisionStay2D (UnityEngine.Collision2D)
0x000002b2aa85eb63 (Mono JIT Code) (wrapper runtime-invoke) <Module>:runtime_invoke_void__this___object (object,intptr,intptr,intptr)
0x00007ffcca5e02f4 (mono-2.0-bdwgc) [mini-runtime.c:3445] mono_jit_runtime_invoke
0x00007ffcca51eb34 (mono-2.0-bdwgc) [object.c:3066] do_runtime_invoke
0x00007ffcca51eccc (mono-2.0-bdwgc) [object.c:3113] mono_runtime_invoke
0x00007ff69e1c80f4 (Unity) scripting_method_invoke
0x00007ff69e1c2bf4 (Unity) ScriptingInvocation::Invoke
0x00007ff69e194ffb (Unity) MonoBehaviour::HandleNotifications
0x00007ff69db47f26 (Unity) GameObject::SendMessageAny
0x00007ff69e8acaeb (Unity) PhysicsContacts2D::SendCallbackReports
0x00007ff69e8aae71 (Unity) PhysicsContacts2D::ProcessContacts
0x00007ff69e87acc8 (Unity) PhysicsManager2D::Simulate
0x00007ff69e8772ef (Unity) PhysicsManager2D::FixedUpdate
0x00007ff69e877436 (Unity) `PhysicsManager2D::Initialize'::`2'::FixedUpdatePhysics2DFixedUpdateRegistrator::Forward
0x00007ff69de730dc (Unity) ExecutePlayerLoop
0x00007ff69de731b3 (Unity) ExecutePlayerLoop
0x00007ff69de78f70 (Unity) PlayerLoop
0x00007ff69edeebbe (Unity) PlayerLoopController::UpdateScene
0x00007ff69edecda2 (Unity) Application::TickTimer
0x00007ff69f25195a (Unity) MainMessageLoop
0x00007ff69f256764 (Unity) WinMain
0x00007ff6a05a9aee (Unity) __scrt_common_main_seh
0x00007ffd4ddf54e0 (KERNEL32) BaseThreadInitThunk
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.