Search Unity

  1. Get the latest news, tutorials and offers directly to your inbox with our newsletters. Sign up now.
    Dismiss Notice

[Solved] Collider2D.OverlapCollider returns 0 and no overlaps are found

Discussion in 'Scripting' started by Fakeyboy, Mar 10, 2019.

  1. Fakeyboy

    Fakeyboy

    Joined:
    Mar 10, 2019
    Posts:
    5
    Hi everyone,

    I have the following situation. I have several sprites which are my Game Tiles. These are my parent game object and they have a Box Collider 2D component as well as a rigidbody 2D component. The parent game object have attached empty child objects. These child objects also have one rigidbody and one Box Collider 2D component. The child objects components lie within the parent object. Here is an example how it looks like for one parent Game Object. All of these objects lie within the same Layer called Default. I hope this makes the situation clear:

    upload_2019-3-10_21-55-53.png

    Now imagine that in Game Play Mode I have several of these parent objects and I postion them in a way so that the Sprites are touching each other which looks something like this:

    upload_2019-3-10_21-57-44.png

    I now want to press a button and detect all overlapping colliders of the child colliders of each parent object. I tried the follwoing code. The parent and child objects as well as their colliders are found, but the overlap method always returns zero:

    Code (CSharp):
    1. private GameObject[] gameTiles;
    2.  
    3.  
    4.     public void NextRoundButtonClicked()
    5.     {
    6.         Debug.Log("Button Clicked!");
    7.         gameTiles = GameObject.FindGameObjectsWithTag("GameTile");
    8.         foreach (GameObject gameTile in gameTiles)
    9.         {
    10.      
    11.             int childCount = gameTile.transform.childCount;
    12.             Debug.Log("Name of Parent Object: " + gameTile.name);
    13.  
    14.             for (int i = 0; i < childCount; i++)
    15.             {
    16.              
    17.                 GameObject childObject = new GameObject();
    18.                 childObject = gameTile.transform.GetChild(i).gameObject;
    19.                 Debug.Log("        Child: " + childObject.name);
    20.                      
    21.                 Collider2D col2d = childObject.GetComponent<Collider2D>();
    22.                 //Create contact filter, maybe have to set it up accordingly
    23.                 ContactFilter2D contactFilter = new ContactFilter2D();
    24.                 //get all overlapping colliders
    25.                 Collider2D[] allOverlappingColliders = new Collider2D[16];
    26.                 /*
    27.                 LayerMask mask = LayerMask.GetMask("Default");
    28.                 contactFilter.SetLayerMask(mask);
    29.                 contactFilter.useLayerMask = true;
    30.                 */
    31.                 int overlapCount = col2d.OverlapCollider(contactFilter, allOverlappingColliders);
    32.  
    33.                 Debug.Log("Overlapcount: " + overlapCount);
    34.                 //some more code
    35.  
    So overlapCount always returns zero. I also tried the same with Physics2D.OverlapCollider as well as Rigidbody2D.OverlapCollider and also tried several setting for the contact filter and nothing works. Where lies the error? If possible I want to avoid creating/casting a box and check for each collider spearatly if it lies within the box. The overlapping method seemed to be an easy way to do it, because in the following code I am looping over each overlapping collider and do some checks.

    Thanks a lot for reading this and I hope someone can help.
     
  2. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    Never worked with those 2d collider, but you only create a contact filter without telling it what to filter. As there is no filter values, it will return in nothing I guess.
     
  3. Fakeyboy

    Fakeyboy

    Joined:
    Mar 10, 2019
    Posts:
    5
    Thanks for your answer. First I thought I do not want to define any filters, In just want to detect each and every overlapping Collider, so I tried it with contactFilter.noFilter() as well as tried to define some filters, like LayerMask and Trigger but nothing seems to work. Maybe I have a wrong understanding of the contactFilter. If I want to filter nothing, so detect everything, how has it to be set up?
     
  4. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    Sorry, cant help you there, as I never worked with this function. Did you try to unparent the colliders or are they unparented that are overlapping? Maybe you can show the hierarchy and inspector. But maybe someone else knows.
     
  5. Fakeyboy

    Fakeyboy

    Joined:
    Mar 10, 2019
    Posts:
    5
    Hi again, my hoerachy looks like the following:

    upload_2019-3-11_19-3-24.png

    upload_2019-3-11_19-3-51.png

    upload_2019-3-11_19-4-18.png

    The same for the other Parent Object. So if they are next to each other it should detect the overlapping, or what am I doing wrong. I also tried to use OverlapBox and created a Box at the Position of the childs collider, like this, also zero results:

    Code (CSharp):
    1. for (int i = 0; i < childCount; i++)
    2.             {
    3.  
    4.                 GameObject childObject = new GameObject();
    5.                 childObject = gameTile.transform.GetChild(i).gameObject;
    6.                 Debug.Log("        Child: " + childObject.name);
    7.                 Collider[] allOverlappingColliders;
    8.                 Collider2D col2d = childObject.GetComponent<Collider2D>();
    9.                
    10.            
    11.                 allOverlappingColliders = Physics.OverlapBox(col2d.bounds.center, new Vector3(0.25f, 0.25f, 0.25f));
    12.                 int overlapCount;
    13.                 overlapCount = allOverlappingColliders.Length;
    14.                 Debug.Log("Amount of Overlaps = " + overlapCount);
    I guess I will just give up then :(
     
  6. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    2,614
  7. Fakeyboy

    Fakeyboy

    Joined:
    Mar 10, 2019
    Posts:
    5
    Hi xVergilx,

    thanks a lot for your answer, that helped a lot. Although I tried the Physics2D.OverlapBoxAll it just found the colliders attached to the Parent Game Object and its own collider but not the colliders of the other game objects when overlapping. But then I tried out other overlapping methods of Physics2D and finally OverlapAreaAll worked and I can find every collider withtin the area. The only thing now is, that this of course also detects the collider which is my template for creating the area, so I just have to filter this out by comparing names and tags of the game object, but before I do that I will investigate further, because I still think it is kind of a filtering issue with the OverlapCollider function. Because the OverlapAreaAll method just uses a LayerMask which is by default DefaultRaycastLayers and if I use this it works. So I think I just need to find a way to configure the contact Filter for the OverlapCollider method in such a way that it also uses this Layer Mask. But currently I have to little knowledge about the Layer Masks I think.
     
  8. Fakeyboy

    Fakeyboy

    Joined:
    Mar 10, 2019
    Posts:
    5
    Hi again,

    I just wanted to thank all of you again for pointing me into the right direction. As I assumed I had some issues with the contact filter. I did a little research and now it works as expected and even better. My parent object colliders are trigger and the childs one are not. I only want to detect all child colliders which overlap a specific child collider. So in case anyone is interested in the solution, with the following code snipped it works perfectly.

    Code (CSharp):
    1.  
    2. Collider2D[] allOverlappingColliders = new Collider2D[16];
    3. Collider2D col2d = childObject.GetComponent<Collider2D>();
    4.  
    5. contactFilter.useTriggers = false;
    6. contactFilter.SetLayerMask(Physics2D.GetLayerCollisionMask(childObject.layer));
    7. contactFilter.useLayerMask = true;
    8.  
    9. int overlapCount = Physics2D.OverlapCollider(col2d, contactFilter, allOverlappingColliders);
     
    dabEnergy and WheresMommy like this.
unityunity