- Home /
Question by
petersenalexander80 · Jul 16, 2020 at 03:25 PM ·
collisioncollider2d game2d-platformerdestroy object
How can I destroy an object if it touches NOTHING
I want to destroy a wall if it doesn't touch another wall upon spawning. That's why I can't use OnTriggerEnter2D. This is the code I tried to use, but it only works if something else collides with the wall.
public class BlockWall_Destroyer : MonoBehaviour
{
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag != "Wall+Ground" | other.gameObject.tag != "Player")
{
Destroy(gameObject);
}
}
}
I need a function that can test if my spawned wall doesn't touch anything.
Comment
Answer by HappyPixel27 · Jul 16, 2020 at 06:12 PM
You will need to create an overlap box, Pretend that its an explosion (look at this video https://www.youtube.com/watch?v=BYL6JtUdEY0). Then make an if statement that checks if there are objects of the tags you are looking for. If there are none then it will destroy.