Spawn Manager : How to detect if there is an prefab on certain location?

I’m creating a spawn manager that creates a random environment.

Now before I place a prefab on a certain place I want to check if there is already a prefab at that location?

My thought was using Physics.CheckSphere() method. But that doesn’t seem to work.
Certain prefabs are still on top of eachother.

private void SpawnTrees()
{
    Vector3 spawnPos;
    for (int index = 0; index < numberOfTrees; index++)
    {
        spawnPos = RandomPosition(xSpawn, zSpawn);
        while (!Physics.CheckSphere(spawnPos, radius))
        {
            spawnPos = RandomPosition(xSpawn, zSpawn);
        }
        Instantiate(tree, spawnPos, tree.gameObject.transform.rotation);
            trees++;
    }
}

for radius I even even use a big number like 5.0f
All my prefabs have a collider box

What am I doing wrong or maybe there is another way?

Physics aren’t the best way to deconflict in this case. It can be made to work but there’s a bunch of issues to consider.

I find it simpler to just keep track of what I’ve spawned and stay away from it. You can see a functioning example in my MakeGeo project, specifically this file:

It is used in the scene by the same name.

MakeGeo is presently hosted at these locations:

https://bitbucket.org/kurtdekker/makegeo

thanks for the help, your principle is working correctly!

In your code you use the SetParent

What’s the use of doing this?

WHEW! :slight_smile:

Keeps things cleaner in the scene, that’s all. Completely optional.