I tried to make a script to make a crate shatter, it worked;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestructibleCrate : MonoBehaviour
{
public GameObject destroyedversion;
private void OnMouseDown()
{
Instantiate(destroyedversion, transform.position, transform.rotation);
Destroy(gameObject);
}
}
but it only worked when I clicked on it I wanted it to have health and destroy it with my gun
so I combined my script to make a cube disappear when its shot but it did not work plz help me
script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class destruction : MonoBehaviour {
public float health = 50f;
public GameObject destroyedversion;
public void takedamage(float amount)
{
health -= amount;
if (health <= 0f)
{
die();
}
}
void die()
{
Instantiate(destroyedversion, transform.position, transform.rotation);
Destroy(gameObject);
}
}
ps. there are no errors