Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Physics.OverlapSphere not detecting Instantiated Objects

Discussion in 'Scripting' started by Enduriel, May 8, 2019.

  1. Enduriel


    Jun 21, 2018
    I'm trying to make a grid (in the future hopefully also other shapes) of Hexes in which all the Hexes find their neighbors by creating a Physics.OverlapSphere and adding all Gameobjects that are within a certain radius (in my case 2 as the radius of the circumcircle of the Hexes is sqrt(3). I instantiate all the Hexes inside of a Hexgrid GameObject
    Code (CSharp):
    1.     private void GenerateMap()
    2.     {
    3.         for (int x = 0; x < size; x++)
    4.         {
    5.             Vector3 pos = gameObject.transform.position;
    6.             if (x % 2 != 0)
    7.                 pos.z -= 1.5f;
    8.             pos.x = gameObject.transform.position.x + 2.25f * x;
    10.             for (int i = (x % 2); i < size * 2; i += 2)
    11.             {
    12.                 GameObject newHex = Instantiate(hexPrefab);
    13.                 pos.z -= 3;
    14.                 newHex.transform.position = pos;
    15.        = x + "_" + i;
    16.                 pos = newHex.transform.position;
    17.                 board[x, i] = newHex;
    18.                 hexes.Add(newHex.GetComponent<Hex>());
    19.                 newHex.transform.parent = transform;
    20.             }
    21.         }
    22.     }
    and then call a GiveNeighbors function to find all the neighbors of a Hex
    Code (CSharp):
    1.     private void GiveNeighbors(GameObject hexObject)
    2.     {
    3.         Collider[] colliderNeighbors = Physics.OverlapSphere(hexObject.transform.position, 2);
    4.         //hexObject.GetComponent<Hex>().makeCircle(2);
    5.         //print(colliderNeighbors.Length + " | " + + " | " + hexObject.GetComponent<Collider>().transform.position);
    7.         foreach (Collider collider in colliderNeighbors)
    8.         {
    9.             if (collider.gameObject.GetComponents<Hex>() != null && collider.gameObject != hexObject)
    10.             {
    11.                 hexObject.GetComponent<Hex>().neighbors.Add(collider.gameObject.GetComponent<Hex>());
    12.             }
    13.         }
    14.     }
    However, Physics.OverlapSphere does return the colliders of any instantiated objects (including a Pawn object that should still be recognized) but it does return the colliders of objects that were placed on the scene before I run the game:
    The List of neighbors for that Hex contains ONLY the hex that I placed there beforehand that you can see hovering slightly above.

    I have no idea how or why this has been happening and after about 4 hours of scouring the internet I've given up and turned to here for any help.

    (I know I could do this differently, but using a Physics.OverlapSphere lets me be very flexible the future shape of my map which is why I'm really trying to make it work like this)

    Any help would be VERY much appreciated.

    EDIT: Added code with tags

    EDIT 2: Final Structure for my GameObjects


    Attached Files:

    Last edited: May 8, 2019
  2. Enduriel


    Jun 21, 2018
    If anyone wants me to try something or even get the source code to try and fix this themselves, message me and I will send it to you because I'm stuck unable to do anything without this working properly.
  3. lordconstant


    Jul 4, 2013
    Things to try:
    Massive overlap sphere, does it get anymore neighbours.
    Place a few manually do they detect each other.
    Do they all have colliders.
    Are rigidbodys needed to be picked up in an overlap shere check?
  4. Enduriel


    Jun 21, 2018
    Ok, got an... odd result trying a big sphere, some of the hexes are detecting ALL of the other 399 Hexes as neighbors (even though they shouldn't in terms of they are too far away) and others are still detecting nothing. After a couple of tries with resizing the OverlapSphere, I noticed that the once that detect all the Hexes that see neighbors should would be within range of the HexGrid GameObject (which Instantiates and then stores all the hexes in an array), even though it doesn't have a collider. Is it possible that the OverlapSphere can only detect the colliders of GameObjects that aren't children?


    When my OverlapSphere is size 8, these Hexes all have 399 or 400 neighbors
    As seen here

    Edit: They do all have colliders
    Edit #2: Correction on the Hexes within range
    Edit #3: Overlapsphere size 6, these Hexes have 399 or 400 neighbors

    Attached Files:

    Last edited: May 9, 2019
  5. ThermalFusion


    May 1, 2011
    Just throwing some theories out there.
    Are you instantiating and spherecasting all within the same frame?
    It may be that the physics engine need another tick before you can reliably spherecast against them.
    Try waiting a physics frame / fixed update after instantiating before spherecasting.
    lordconstant likes this.
  6. Baste


    Jan 24, 2013
    @ThermalFusion is probably correct in that it's due to instantiating and spherecasting in the same frame. You don't have to skip a frame, though, you just have to tell the physics engine to sync transforms, by calling Physics.SyncTransforms.
    ThermalFusion likes this.