Why IsPointerOverGameObject return null

Ok, I have this Canvas with 3 buttons.
When i touch 1 button (mean Throttle) in console output name of GameObject (ThrottleButton). It’s correct.
Same on 3 button. But if i click on 2 ui element ( it’s part of “Simple Joystick Pack”) in console output Null.
i.e. if i click on 2 button EventSystem.current.currentSelectedGameObject is Null.

My question - why?

There one different between buttons. UI elements one and two have “Button” component.

void FixedUpdate()
    {
        foreach ( Touch touch in Input.touches )
        {
            if ( EventSystem.current.IsPointerOverGameObject( touch.fingerId ) )
            {
                Debug.Log( touch.fingerId );
                Debug.Log( EventSystem.current.currentSelectedGameObject );
            }
        }
    }

This has nothing to do with button component. Object under your finger are detected using UI raycaster and it have a ruleset to determine if there’s object or there’s not. For example, that (2) object may contain a canvas group marked as non-interactable, or there migh be other reasons. It hard to tell without knowing that component you use, but you may chek EventSystem.current.RaycastAll method results to see what object are detectable.

1 Like

Hmm, need to test it! Thanks.

Ok, I solve it

        // Условие для проверки того, попал ли тач в интерфейс и в какой именно.
        // Если тачей больше 0, то для каждого тача проверяется, прошел ли он через UI
        // Если да, то проверяется что это за UI элемент с помощью EventSystem.current.RaycastAll
        // Если это нужная кнопка - то делаем действия, например  увеличиваем тягу или делаем поворот
        if ( Input.touchCount > 0 )
        {
            foreach ( Touch touch in Input.touches )
            {
                if ( EventSystem.current.IsPointerOverGameObject( touch.fingerId ) )
                {
                    PointerEventData pointer = new PointerEventData(EventSystem.current);
                    pointer.position = touch.position;

                    List<RaycastResult> raycastResults = new List<RaycastResult>();
                    EventSystem.current.RaycastAll( pointer, raycastResults );

                    if ( raycastResults.Count > 0 )
                    {
                        foreach ( var go in raycastResults )
                        {
                            Debug.Log(go.gameObject.name);
                        }
                    }
                }
            }
        }
2 Likes