- Home /
Destroying a square of sprites at specific coordinates
Hello, I am working a medium 2d mining game and I have a script that makes a custom-sized square world, but I would like to clear a square in the middle and clear spaces for random ores with only the coordinates and thae name of the object. I now that there is a way to delete a GameObject at a specific location but it is far too big to be used when deleting multiple objects. Here is my function for creating the square world:
function MakeRock() {
Instantiate(Rock, Vector2(0, 0), Quaternion.identity);
Instantiate(Ground, Vector3(0, 0, 0.1), Quaternion.identity);
while (-FillUp * 2 <= Size) {
while (up < Size) {
Instantiate(Rock, Vector2(FillUp, CountUp), Quaternion.identity);
Instantiate(Ground, Vector3(FillUp, CountUp, 0.1), Quaternion.identity);
CountUp = CountUp + 0.5;
up++;
}
FillUp = FillUp - 0.5;
up = 0;
CountUp = 0.5;
}
while (FillDown * 2 <= Size) {
while (down < Size) {
Instantiate(Rock, Vector2(FillDown, CountDown), Quaternion.identity);
Instantiate(Ground, Vector3(FillDown, CountDown, 0.1), Quaternion.identity);
CountDown = CountDown - 0.5;
down++;
}
FillDown = FillDown + 0.5;
down = 0;
CountDown = -0.5;
}
while (FillRight * 2 <= Size) {
while (Right < Size) {
Instantiate(Rock, Vector2(CountRight, FillRight), Quaternion.identity);
Instantiate(Ground, Vector3(CountRight, FillRight, 0.1), Quaternion.identity);
CountRight = CountRight + 0.5;
Right++;
}
FillRight = FillRight + 0.5;
Right = 0;
CountRight = 0.5;
}
while (-FillLeft * 2 <= Size) {
while (Left < Size) {
Instantiate(Rock, Vector2(CountLeft, FillLeft), Quaternion.identity);
Instantiate(Ground, Vector3(CountLeft, FillLeft, 0.1), Quaternion.identity);
CountLeft = CountLeft - 0.5;
Left++;
}
FillLeft = FillLeft - 0.5;
Left = 0;
CountLeft = -0.5;
}
}
Thank you, Qo2770
Comment
Try storing your sprites in a three-dimensional Array (e.g. Generic List), if you are using voxels like $$anonymous$$C. If you are not, then you might think of some structure like an Oct-tree, or use a Hash$$anonymous$$ap(e.g. Dictionary) based on the position of your GameObjects.