1
\$\begingroup\$

I've made a simple script which makes a mesh with a collider act like a button. However the mesh button get clicked through the UI. I figured out how to fix this in editor, however this problem still occurs on the android device (did not test iOS)

    using UnityEngine;
    using UnityEngine.Events;
    using UnityEngine.EventSystems;

    public class MeshButton : MonoBehaviour {
        public bool Interactable = true;
        public UnityEvent onClick;

        private void OnMouseDown() {
            if (Interactable) {
                if(IsPointerOverGameObject()) return;
                onClick.Invoke();
            }
        }

        private bool IsPointerOverGameObject()
        {
#if UNITY_ANDROID || UNITY_IPHONE
            Touch touch = Input.touches[0];
            return EventSystem.current.IsPointerOverGameObject(touch.fingerId);
#else
            const int fingerId = -1;
            return EventSystem.current.IsPointerOverGameObject(fingerId);
#endif

        }
    }
\$\endgroup\$
  • \$\begingroup\$ Try adding another check to IsPointerOverGameObject: return EventSystem.current.IsPointerOverGameObject() && EventSystem.current.currentSelectedGameObject != null \$\endgroup\$ – eLTomis Apr 27 at 7:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.