How to detect multiple/Overlapping GameObjects with the EventSystem?

Viewed 2322

What I try to achieve: Toothbrush should show up wherever the user clicks inside BoxCollider A, including space inside BoxCollider B. But apparently clicking inside B will not show the toothbrush (OnPointerDown is not being triggered).

What I have tried: Changing the order of the layers.

Toothbrush is showed after user clicks inside box collider A, but if the user clicks inside box collider B- the toothbrush will not show up, which means OnPointerDown is not triggered.

I think it is because of the overlapping of one BoxCollider2D inside another BoxCollider2D. In my case B inside A, I assume that is the culprit, but I have no idea how to solve it or if maybe there is another method to implement OnPointerDown?

I am using Perspective camera. but in this scene all elements are in same z position which is 0. Is it possible to trigger IPointerHander event in every respective BoxCollider2D ?

enter image description here

DragableObject.cs

This script is attached to the toothbrush. The BoxCollider2D A also belongs to toothbrush.

public void OnPointerDown(PointerEventData eventData)
{
    Debug.Log("pointer down");

    if (GetComponent<DragableObject>() == null)
        return;

    currentObject = GetComponent<DragableObject>();

    MeshRenderer renderer = GetComponent<MeshRenderer>();

    if (ShowOnTouch)
        ShowObject();

    // Store original state
    originalPosition = transform.position;
    originalOrderLayer = renderer.sortingOrder;
    // Snap to mouse
    Vector3 newPos = cam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 30));
    newPos.z = 30;
    transform.position = newPos;

    if (BringToFront)
    {
        if (renderer != null)
        {
            renderer.sortingOrder = 90;
        }
    }

    ObjectActive.Invoke();
}

TargetListener.cs

This script is attached to BoxCollider2D B.

public void OnPointerDown(PointerEventData eventData)
{
    for (int i = 0; i < Affectors.Count; i++) 
    {
        if (Affectors [i] == DragableObject.currentObject)
        {
            DragableObject.currentObject.OnEnterTarget(transform);

            ITriggerEffect[] iTrigger = GetComponents<ITriggerEffect>();

            for (int j = 0; j < iTrigger.Length; j++) 
            {
                Debug.Log("iTrigger enter");
                Debug.Log(iTrigger [j]);
                iTrigger [j].Execute(eventData, PointerState.Down);
            }
        }
        else
            continue;
    }
}

If i click on A the toothbrush will show up, except when i click inside B. Here the debug log.

enter image description here

This is the BoxCollider2D A is attached to, which is the *Toothbrush itself together with the dragable.cs script.

enter image description here


UPDATE: Thanks to others who answers, the problem become more clearer for me. Below is BoxCollider2D A and BoxCollider2D B. Both of them have script that have most of OnPointerHander. How do i make sure that all OnPointerHandler is triggered on respective BoxCollider2D ?.

enter image description here

Issues that i am having :

  1. OnPointerExit on A is triggered when my pointer enter B.
  2. if click inside B ,OnPointerDown only triggered on B but not A
2 Answers
Related