- Home /
Physics.OverlapSphere() not working after using pool of objects
Hello, I`m trying to spawn surfaces in my endless runner. I want them to be spawned around my current surface (left, leftTop, top, rightTop and right positions (I don`t need bottom positions)). Everything worked perfect we I used Instantiate() method to add a new surface, but it was too expensive, so I decided to create a pool of surfaces. I used Physics.OverlapSphere() to detect whether a surface was spawned it certain position, but after I started using pool of surfaces the method stopped detecting collisions.
 //Old logic of spawning objects using Instantiate
 Update
 {        
 ...
 randomType = (int)Mathf.Round(Random.Range(0, objectsToSpawn.Count));
         intersecting = Physics.OverlapSphere(rightAreaPos, 0.02f);
         InstantiateObject(intersecting.Length == 0, rightAreaPos, objectsToSpawn[randomType]);
 ....
 }
 
  private GameObject InstantiateObject(bool allowedToSpawn, Vector3 position, GameObject objectToSpawn)
     {
         if(allowedToSpawn)
         {
             return GameObject.Instantiate(objectToSpawn, position, objectToSpawn.transform.rotation);
         }
         return null;
     }
 //new logic using pool
 Update 
 {
 ...
 randomType = (int)Mathf.Round(Random.Range(0, surfacesPool.AmountOfPooledObjects));
         intersecting = Physics.OverlapSphere(rightAreaPos, overlapRadius);
         instantiatedObj = InstantiateObject(intersecting.Length == 0, rightAreaPos, randomType);
 ...
 }
 
   private GameObject InstantiateObject(bool allowedToSpawn, Vector3 position, int randomType)
     {
         if(allowedToSpawn)
         {
             GameObject surface = surfacesPool.Pull(randomType);
             surface.transform.position = position;
             return surface;
         }
         return null;
     }
 
 //Surface Pool
     public GameObject Pull(int index) 
     {
         if(pooledObjects.Count > 0)
         {
             index = index % pooledObjects.Count; // to avoid index out of range
             GameObject gameObj = pooledObjects[index];
             pooledObjects.Remove(gameObj);
             gameObj.SetActive(true);
             return gameObj;
         }
         return null;
     }
Why Physics.OverlapSphere() stopped detecting collision after I started setting the needed position of already instantiated object?
Your answer
 
 
             Follow this Question
Related Questions
collisions in character not working! 2 Answers
How to Destroy a gameobject on collision 3 Answers
[Solved] How to get non repeating random positions? 5 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                