- Home /
Find object with tag in small distance and delete found object and itself.
Hello! So I have this code, that randomly spawns pipes and then deletes the transform it spawned at, so that something doesn't spawn there again, this works. There is a problem, however:
If there is a pipe with 4 openings, when spawned that would leave 3 openings. The problem here is that it doesn't check if there is a connection (So with tags, for example Right would connect to Left)
How do I check if there is something in range, while also not checking the whole world for objects with some tag, then check if one of those are in range. I don't want this because it is inefficient, logical I hope? I cannot check if there is ANY sort of pipe in any direction, because some pipes don't have a connection that way, and that would leave an open pipe.
Answer by logicandchaos · Oct 12, 2021 at 02:25 PM
You can use OverlapCircleAll for 2d or OverlapShereAll for 3d then you check the tags, then delete the object.
Alternatively you could use FindGameObjectWithTag and then check the distance with Vector2.Distance for 2d or Vector2.Distance for 3d.
I think the 1st may be more efficient, but really depends on a lot of factors.
Alright, so I figured that just using this:
void OnTriggerStay2D(Collider2D col)
{
Destroy(col.gameObject);
Destroy(gameObject);
}
Would work, only it doesnt. Would you have any clue why? 
The colliders are on each of those children (The ones with anchors), using this should destroy bottom and right transforms? The top one is gone, because that is where it spawned.
I found the answer! I thought something was wrong with my colliders, but no, it was them defor$$anonymous$$g and not touching because they were anchored children.
Your answer
Follow this Question
Related Questions
Unity 2D - Pivot points based on slice, not on original texture. 0 Answers
Camera Effects for 2d 0 Answers
Unity 4.3, generate 2d mesh 2 Answers
2D C# Jump Script Addforce 2 Answers
Is there any way of creating primitive 2D shapes in unity? 1 Answer