How to Destroy A GameObject when it enters any Triggers
Hello There i need a GameObject Called Bullet to Destroy on Enter of any Trigger here is what i got so far
#pragma strict
var Bullets : GameObject;
var Trg : GameObject;
function OnTriggerEnter (Trg : Collider) {
Destroy(Bullets.gameObject);
}
Answer by nirto_pineapplez · Jun 27, 2016 at 01:47 PM
make a java script file and copy and paste this in to it:
#pragma strict
function OnTriggerEnter (other : Collider)
{
if(other.tag == "Collider")
{
Destroy(gameObject);
}
}
and then place the script onto the bullet and make the collider on the bullet a trigger. hope this helps.
Thanks $$anonymous$$an i Changed Collider to Enemy with the tag on the Object i was shooting at and it works
Answer by Cherno · Jun 27, 2016 at 12:28 AM
So, the bullet GO needs to be destroyed when it enters any trigger collider?
Then put this script on your Bullet GO:
function OnTriggerEnter (collider : Collider) {
Destroy(gameObject);
}
Note that: 1. The bullet needs a non-trigger collider and a non-cinematic RigidBody. 2. The other GO needs a convex collider with IsTrigger set to true. 3. Both GOs have to be in layers that are set to register collisions (Physics Settings - Collision Matrix) 4. The Bullet GO may not travel through space too fast or collisions won't register reliably (when a GO moves from A to B between two frames but a collider is between it, there won't be any contact between the two). In that case, Search for "bullet registers no collision" or similar, lots of solutions for this problem.
Your answer
![](https://koobas.hobune.stream/wayback/20220612083732im_/https://answers.unity.com/themes/thub/images/avi.jpg)