- Home /
Destroy object on touch of object with specific class
I need to know how to destroy a 2d object named enemy with the enemy when a 2d object named bullet with the bullet tag hits it. I am also new, please explain things as much as you can.
Answer by thanedurey · Mar 17, 2019 at 09:58 PM
OnCollisionEnter2D is the function you might want to use.
In your bullet class, you would add this void OnCollisionEnter2D function (below).
Then, you'll need to add some kind of Collider on your bullet (Box Collider 2D most likely) and set the Collider in your inspector to isTriggered.
Finally, you'll need Colliders on your objects you'll want to destroy. So that when your bullet collides with the object, the below function will get triggered.
This 5 min video might also help you. (link)
void OnCollisionEnter2D(Collision2D coll)
{
UnityEngine.Debug.Log("Colliding on object: "+ coll.gameObject.tag);
if (coll.gameObject.tag == "Your tagged item")
{
Destroy(coll.gameObject);
}
}
You can also use Destroy(coll.gameObject, 1) to destroy the object after 1 second.
I can’t get it to work, the bullet’s name is rocket and the object I want to destroy is named enemy
What does your debugger say in your OnCollisionEnter2D?
Never $$anonymous$$d, I got it to work, thank you for your help.
Answer by Tsaras · Mar 17, 2019 at 10:00 PM
An easy way to do it is check in the bullet's Update loop to see whether or not it is close to the other object. This works well if you have a way to loop through all the objects that the bullet could be hitting.
In bullet's script:
void Update(){
float distance = Vector3.Distance(transform.position, object.transform.position); //object is the object you want to check for hit
if (distance < .05f) { //some small number
Destroy(object);
}
}
Another way to do it which spares you the hassle of getting a list of objects to check is using a trigger collider with the OnTriggerEnter method on the object:
void OnTriggerEnter(Collider other){
if (other.gameObject.CompareTag("bullet")){
Destroy(gameObject);
}
}
I’ll let you know if this works when I can get back to it.
Answer by NatCou · Mar 17, 2019 at 11:11 PM
some scribble ideas
void OnTriggerEnter(Collider otherThing)
{
if (otherThing is Component)
{
MyClass = otherThing.GetComponent<MyClass>();
MyClass.doSomething();
}
}
//some other idea also
//private Character myCharacterDefinedinHeaderAsInstance;
//later....
private void OnCollisionEnter(Collision col)
{
if (col.gameObject.GetComponent<Character>())
{
Debug.Log("Character Hit");
myCharacterDefinedinHeaderAsInstance = col.GetComponent<Character>();
myCharacterDefinedinHeaderAsInstance.doSomething();
}
}
https://answers.unity.com/questions/1323996/check-if-object-is-of-type-component.html
I’ll see if this works as quickly as I can. Which is probably in the morning CST.
Your answer
Follow this Question
Related Questions
Simple on Collision Help (C#) 2 Answers
destroying gameobject from other script 1 Answer
Collision on specific Frames 1 Answer
[c#]collision script not working 1 Answer
Destroy temp object and make new object. 0 Answers