Destroy crate with animation

Hey guys im trying to create a crate that my character can destroy and then a animation starts playing from the crate getting destroyed but for some reason my crate gets destroyed before the animation can start playing please help me fix this issue:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Crates : MonoBehaviour
{
    public Rigidbody2D rb;
    private Animator anim;
    public bool crate;

    void Start()
    {
        rb = gameObject.GetComponent<Rigidbody2D>();
        anim = gameObject.GetComponent<Animator>();
        anim.SetBool("crate", false);
        crate = false;
    }

    void Update()
    {
        if (crate)
        {
            anim.SetBool("crate", crate);
        }
    }

    void OnCollisionEnter2D(Collision2D col)
    {
        if (col.gameObject.tag == "Player")
        {
            crate = true;
            Destroy(transform.parent.gameObject);
        }

    }
}

destroy will remove the object at the end of the frame it’s called, so you’re not letting the animation play through. Probably the simplest approach for this would be to have destroy called from an animation event

https://docs.unity3d.com/Manual/animeditor-AnimationEvents.html

Thx for your response but that’s not what i meant the animation is working fine the problem that i have is that i destroy the object before the animation can even start…

Destroy(transform.parent.gameObject, 5f);

You can just try this to wait 5 seconds before destroying (or however long your animation is). Destroy has an optional parameter “time” that the engine waits before destroying that object.

Thank you so much that’s exactly what i was looking for!!