- Home /
Destroy Gameobject without BoxCollider2D
Hi, I have this code:
the script works with objects with boxcollider2D components but there are some objects who doesn't have, is there a way to destroy everything the destroyer touches, Thanks for your help:
using UnityEngine;
using System.Collections;
public class Destroyer : MonoBehaviour
{
void OnTriggerEnter2D(Collider2D Other)
{
if(Other.gameObject.tag == "Untagged");
{
Destroy(Other.gameObject);
}
}
}
Answer by revolute · Aug 21, 2014 at 02:32 AM
This means you will have to do every calculation by your own script. All objects have transform components, so you will have to calculate the distance between objects check if they collide. I'm guessing you don't want to do this but I don't think there is alternative. This is a primitive way to check collision, used mostly when people create their own program without too much api.
Hi thanks for your answer, I guess I'll have to find another way since the speed isn't constant
Answer by alebasco · Aug 21, 2014 at 01:40 AM
That code should work with any 2D colldier.
However, there is no way to check collision with an object that does not have a collider. Unity does not know they are colliding, and OnTriggerEnter is never called. As no collider has entered the Destroyer's collider.
Why not give the other objects that you need to destroy colliders?
Hi, thanks for your anwser, I cant add a boxcollider since I want to delete a background which keeps spawning (like an infinite runner) and then dissapear of the screen, is there a way to achieve it without a boxcollider2D?
You could add it as a box collider with a trigger.
Alternatively, if you are spawning more and more backgrounds - don't delete them! Once they go off screen, move them to the front of the line ins$$anonymous$$d of spawning new ones. That will be more efficient as well. To check to see if an object is visible, try renderer.isVisible. (Note, if ANY camera can see it, then it will be true. This includes scene view)
Thanks! that's a really good idea, I'll try both of them. thanks for your help alebasco.
Your answer
![](https://koobas.hobune.stream/wayback/20220613161123im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
How to destroy and trigger on enter. 1 Answer
How do I call an on trigger enter / destroy gameObject in the scene c# 1 Answer
OnTriggerEnter Influencing all GameObjects of the Same Type 2 Answers
Why won't the coin destroy when hit? 0 Answers