collider2D.bounds.Contains not working properly

I’ve got a GameController object with a square 2D collider that covers the whole screen. Inside that GameController there are 7 objects (Zones), each one with their own polygon collider. Here’s the setup:

43122-2.png

What I’m trying to do is to check if the clicked position is inside any of those Zone’s colliders whenever I click inside the big square collider.

This is the OnMouseDown() code of the GameController’s script:

void OnMouseDown ()
{
    Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

    if(this.collider2D.bounds.Contains(mousePos)) Debug.Log ("1st Check");

    //'Zones' is a list with all the Zones GameObjects
    foreach (GameObject zone in Zones)
    {
        if(zone.collider2D.bounds.Contains(mousePos))
        {
            Debug.Log ("2nd Check");
        }
    }
}

Shouldn’t I ALWAYS get the “1st Check”? Because it’s redundant: if the OnMouseDown() got triggered because I clicked inside the collider, then the mouse position has to be inside the collider. But I never do, so I think I’m missing something right here about the Input.mousePosition or the ScreenToWorldPoint()

Haven’t tested, but it might be because you’re trying to see if a 2D collider contains a 3D point. Cast your mousePos ((Vector2) mousePos) when you call the contains and see if that works.

Also, collider.bounds doesn’t work so well with non-rectangular colliders. So for your zones, you may want to use collider.OverlapPoint instead. (And also you may want to fix up those colliders to match the visual representation.)

if you just want to chek you are clicking on zona than you dont need to use collider.bounds

just write in script

//this will give you event when you click on zona 1 if its drag on zona1

void OnMouseDown()

{
debug.log(“clicked on zona1”); //put this script on zona1 gameobject and same for all 7
}

This should be a unity bug. If I use Collider2D and check whether another object is inside it, I should not consider z-position, should I?