Check for object before spawn

I made a game where you can spawn multiple objects that can move. but i want to make sure that the spawned object doesnt spawn on a player or a part of the terrain. and if it can’t spawn i want a new spawnrange. this is my code:

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Mouse1))
        {
            // Makes a random Vector3 and puts it in the spawner. f after numbers means decimal number.
            Vector3 spawnrange = new Vector3(Random.Range(-10, 11), Random.Range(-5, 6), 0);
            // Instantiate at a random position and zero rotation.
            Instantiate(player, spawnrange, Quaternion.identity);
          
        }
    }
}

You could cast a ray straight down where you want to spawn your player. If it hits anything, you will try to find a new spawn.

The physics class as a number of ‘Overlap’ methods that you can use to check if anything is within a particular area: Unity - Scripting API: Physics

My MakeGeo project has some object spawn deconfliction examples. Search for “deconflict” in the project.

MakeGeo is presently hosted at these locations:

https://bitbucket.org/kurtdekker/makegeo