Check if the position taken

i spawn object every seconed in random place.
i want to check if the new object`s points are near to another object or same location.

while ((spawnY == gmBonus.transform.position.y && spawnY == gmBonus.transform.position.x) || ((spawnY <= gmBonus.transform.position.y + 2f) && (spawnY >= gmBonus.transform.position.y - 2f)) || ((spawnX <= gmBonus.transform.position.x + 2f) && (spawnX >= gmBonus.transform.position.x - 2f)));

This is the line of code that checks it. if is true so take other points. It should work!
My problem is That it checks for one object in the scene.
if i have 1000 objects in my scene? the line code chack just to last object that spawnded and not to all objects in the scene…

this is the function:

    private void Spawn()
    {
        gmBonuses = GameObject.FindGameObjectsWithTag("Bonus");
        foreach (GameObject gmBonus in gmBonuses)
        {
            if (gmBonus != null)
            {
                do
                {

                    spawnY = Random.Range(Camera.main.ScreenToWorldPoint(new Vector2(0, 0)).y,
                        Camera.main.ScreenToWorldPoint(new Vector2(0, Screen.height)).y);
                    spawnX = Random.Range(Camera.main.ScreenToWorldPoint(new Vector2(0, 0)).y,
                         Camera.main.ScreenToWorldPoint(new Vector2(0, Screen.height)).y);
                } 
                while ((spawnY == gmBonus.transform.position.y && spawnY == gmBonus.transform.position.x) ||
                        ((spawnY <= gmBonus.transform.position.y + 2f) && (spawnY >= gmBonus.transform.position.y - 2f)) ||
                        ((spawnX <= gmBonus.transform.position.x + 2f) && (spawnX >= gmBonus.transform.position.x - 2f)));

            }
        }
        
        Vector2 spawnPosition = new Vector2(spawnX, spawnY);
        Instantiate(bonus[0].bonusPrefab, spawnPosition, Quaternion.identity);
    }

i tried to check with foreach and with tags but It’s the same effect…

Hello.

Instead of complex scripting, best solution is to have a “object detector” as a prefab so you can instantiate it where you want to spawn, and if doesnt detect any other object of the specyfic type, continue with the spawn.

this detector shpuld be only an emptyobject with a triggercollider and a script.

Bye!