Overlap Sphere logic causes Unity Editor to Freeze

Hi All!

I’m trying to just place a whole bunch of game objects but avoiding having them be inside one another. I’m using an OverlapSphere to check each position for collisions before I actually move the gameobject into place. However, when the Method, GenerateMidSpawnPosition(), gets called, the Editor freezes.

Anybody have any ideas on what I’m doing wrong? And if so, if you have a solution, that would be fantastic.

There’s some upper case comments around what seems to be the offending area.

Thanks in advance.

    public void MidLevelRestart()
    {
        Debug.Log("MidLevelRestart has been called");

        levelSpawn_Manager.currentNumberSpawned = 0;
        levelSpawn_Manager.totalToSpawn = numberOfMinesCurrent + numberOfSeekersCurrent;

        levelSpawn_Manager.player.transform.position = levelSpawn_Manager.playerStartPosition; //move player to the center of the level

        survivors = new List<GameObject>(GameObject.FindGameObjectsWithTag("MarbleMine"));//create a list to be used for placing the enemies and add first type of enemy
        survivors.AddRange(new List<GameObject>(GameObject.FindGameObjectsWithTag("Seeker")));//add to the list with the second type of enemies

        while (levelSpawn_Manager.currentNumberSpawned <= levelSpawn_Manager.totalToSpawn - 1)
        {
            GenerateMidSpawnPosition();
        }

    }

    public void GenerateMidSpawnPosition()
    {
        float newX = Random.Range(-50f, 50f);  //random point within the width of game area
        float newZ = Random.Range(-40f, 40f);  //random point within the depth of game area
        midStartPosition = new Vector3(newX, 1, newZ); //generated Vector3 that will be tested with the overlapSphere and if good, a game object will be placed there.
        float distanceToPlayer = Vector3.Distance(midStartPosition, levelSpawn_Manager.playerStartPosition);  //How far is this point away from the player?

        if (distanceToPlayer < 5.0f) //if point is less than the min distance to player, ...
        {
            GenerateMidSpawnPosition(); //...try another point           
        }

        else if (distanceToPlayer > 5.0f) //if the point is more than the minimum distance away from the player, check to see if it's colliding with an existing game object...
        {
            
            //THIS IS THE OFFENDING LINES OF SCRIPT FROM HERE...
            Collider[] hitColliders = Physics.OverlapSphere(midStartPosition, 3f, thisMask);  //thisMask is set to the ground which would get hit everytime on placement of gameobjects
            {
                if (hitColliders.Length == 0)//if this point isn't inside another game object, ...
                {
                    PlaceEnemies(midStartPosition);  //place the enemy
                    Debug.Log("Overlap Collider did not hit something");
                }
                else if (hitColliders.Length > 0) //if length is greater than 0, you are colliding with something...
                {                   
                    GenerateMidSpawnPosition(); //...so get another point.
                    Debug.Log("Overlap Collider hit something");
                }
            }
            //...TO HERE.
            
        }
    }

    public void PlaceEnemies(Vector3 placementPoint)
    {
        survivors[levelSpawn_Manager.currentNumberSpawned].transform.position = placementPoint;
        levelSpawn_Manager.currentNumberSpawned++;
    }

Pretty sure just a case of infinite-loop thanks to faulty exit-conditions…Check if the exit condition of this while loop is updating correctly:

while (levelSpawn_Manager.currentNumberSpawned <= levelSpawn_Manager.totalToSpawn - 1) //<<< Error is probably here
{
    GenerateMidSpawnPosition();
}

Thanks for the quick input XenoRo. I ended up scrapping what I was doing and used a SphereCast instead and it seems to be working.

 public void GenerateMidSpawnPosition()
    {
        float newX = Random.Range(-50f, 50f);  //random point within the width of game area
        float newZ = Random.Range(-40f, 40f);  //random point within the depth of game area
        midStartPosition = new Vector3(newX, 1, newZ); //generated Vector3 that will be tested with the overlapSphere and if good, a game object will be placed there.
        float distanceToPlayer = Vector3.Distance(midStartPosition, levelSpawn_Manager.playerStartPosition);  //How far is this point away from the player?
        Debug.Log("Distance to player mid start position"+distanceToPlayer);
        if (distanceToPlayer < 5.0f) //if point is less than the min distance to player, ...
        {
            GenerateMidSpawnPosition(); //...try another point    
            Debug.Log("Logic for less than distancetoplayer is being called");
        }

        else if (distanceToPlayer >= 5.0f) //if the point is more than the minimum distance away from the player, check to see if it's colliding with an existing game object...
        {
            //PlaceEnemies(midStartPosition);
            //Debug.Log("Logic for greater than distancetoplayer is being called");
           
            RaycastHit hit;

            if (Physics.SphereCast(midStartPosition, 3f, transform.up, out hit, 3f, thisMask))
            {
                GenerateMidSpawnPosition();
            }

            else
            {
                PlaceEnemies(midStartPosition);
            }            
        }
    }