Hey im trying to make it so that if my player jumps on a box it explodes and you hear a explosion sound effect but for some reason my code seems to be not working please let me know what i am doing wrong:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Crates : MonoBehaviour
{
public Rigidbody2D rb;
private Animator anim;
public bool crate;
public AudioClip explosion;
private AudioSource audioSource;
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;
GetComponent<AudioSource>().Play();
Destroy(transform.parent.gameObject, 0.7f);
audioSource = GetComponent<AudioSource>();
audioSource.clip = explosion;
audioSource.Play();
}
}
}