- Home /
I'm trying to get an enemy plane to explode when hit by a projectile but it doesn't work
I am using an empty game object with a triggered collider at the back of the plane (where I hit)
I am using this script:
using System.Collections;
using UnityEngine;
public class explode: MonoBehaviour {
public GameObject explosion;
void OnTriggerEnter(Collider other)
{
if (other.tag == "bullet")
{
Destroy (transform.parent.parent.gameObject);
Instantiate(explosion, transform.position, transform.rotation);
}
}
}
I attributed the script to the game object. When I crash my plane into the collider it explodes but when I shoot at it with a bullet it doesn't. I don't know if this is because the shot bullets are clones.
Answer by Labertasche3_0 · May 06, 2020 at 10:10 AM
Do the bullets have correct colliders? Do they have the tag?
Also, have you tried doing if (other.gameObject.tag == "bullet")
Also, if your bullet is moving to fast it could be that collisions don't work correctly.
Yeah maybe, but I don't know how to add a tag. Is it the same thing as the prefab name?
Answer by ashkanaral · May 07, 2020 at 01:31 AM
Maybe transform.parent.parent is wrong. Check this out https://docs.unity3d.com/ScriptReference/Transform.SetParent.html It shows that the Destroy changes for the gameobject when false. Possibly inside ( ) your Destroy statement is false?
Your answer
![](https://koobas.hobune.stream/wayback/20220612233430im_/https://answers.unity.com/themes/thub/images/avi.jpg)