How to: Dont spawn on other objects ?

Hi,

i want some prefabs to spawn at a point where no collider-object is located.

my spawn function:

    public void SpawnSpeedup()
    {

        int x = (int)Random.Range(border_l.position.x, border_r.position.x);
        int y = (int)Random.Range(border_t.position.y, border_b.position.y);

        Instantiate(speedup_prefab, new Vector2(x, y), Quaternion.identity);

    }

How can i do this ?

My question is similar to this: Collision spawned object - Questions & Answers - Unity Discussions

Two main ways:

1: Use raycasts to check if the spot is already occupied.

2: Add all possible spawn points to a list, select a random element from that list as a spawn point, and remove each selected spawn point from the list.

Use the first one if you also want to avoid pre-placed objects, use the second one otherwise (as it’s probably easier to get right).