- Home /
Destroy OTHER objects on Collision Unity
I found a BUNCH of questions and comments about destroying an object if it is hit by something but in my scene, I want the PROJECTILE to be destroyed. How do I make it that anything that collides with an object gets destroyed? It's sorta like a reverse bullet. c# please
Answer by Cherno · Oct 06, 2015 at 10:18 PM
For a collision to register, the following needs to be true:
The objects needs a non-kinematic rigidbody and a non-trigger collider.
The other object needs a non-trigger collider.
One of the colliders needs to be convex.
Both objects needs to be in layers that are set up to register collision (project settings -> physics).
For a Trigger Collision to register, the other object needs a convex collider set to IsTrigger = true. Note that Addyarb posted an example that uses OnTriggerEnter. For a normal collision, you would use OnCollisionEnter():
void OnCollisionEnter(Collision collision){
Destroy(collision.collider.gameObject);
Destroy(gameObject);
}
Bonus tip: Insert Debug.Log lines to see which parts of your code are called.
Answer by Addyarb · Oct 06, 2015 at 10:08 PM
Here's both ways to destroy the gameObject, or the gameObject you collide with.
void OnTriggerEnter(Collider col){
Destroy(col.gameObject);
}
void OnTriggerEnter(){
Destroy(gameObject);
}
So in the second one, it destroys the object that is hit, right? And in the first it destroys the objects that hits it, right? Or do I have them mixed up? I'll try them both. Thank You
uh oh...Nothing seems to be happening at all. Neither of them are being destroyed. Hmm...The projectile has a rigid body but the object to be hit doesn't. Is that a problem?
Yeah try 'em out!
Actually the parameter that you're supplying in the OnTriggerEnter method is the object you're hitting. You can call it whatever you want to make it easier. For instance:
void OnTriggerEnter(Collider TheColliderThatIWillBeCollidingWith){
if(col.tag == "Target"){
Destroy(TheColliderThatIWillBeCollidingWith.gameObject);
}
}
You'll need to put a collider with "is Trigger" enabled as well for OnTriggerEnter :)
Also as Cherno said - you'll need to have one object with a non-trigger collider, and one with. In this case - you are calling OnTriggerEnter(Collider col) which basically means you are checking THIS object's script for a collision on its trigger collider - and supplying the OnTriggerEnter method with a parameter which is the object that you collided with.
In fact, it's essentially setting a temporary reference to the object you collided with - so you can even access components, transforms, tags, etc. once you collide with it.
i.e.
col.GetComponent<Script>().scriptIntVariable = 10;
Answer by neevcuber · Jun 08, 2018 at 01:56 AM
to make the OTHER destroying stuff you told copy this code:
public Transform gameobject;
void OnCollisionEnter(Collision coll) {
if (coll.gameobject.name == mycube) {
Destroy (gameobject);
}
}
Your answer
Follow this Question
Related Questions
Strange Collisions (...as there should be none!) 0 Answers
Specify collision notification order? 0 Answers
any way to get a pre-solve collision callback? 3 Answers
OnCollisionExit2D workaround? 0 Answers
OnColliderEnter2D not working 2 Answers