- Home /
UI System not blocking Raycasts on Mobile only
I am having problems getting the new UI system to block raycasts on mobile devices.
EventSystem.current.IsPointerOverGameObject()
This works 100% perfectly on desktop but does not work on touch screen devices. I've tried adding a cube behind my UI elements to block raycasts, Canvas Groups with the blocks raycast option checked and while it all seems to work great on desktop I cannot get it to work the same way on mobile touch screen devices.
I should add I am using Navmesh and Navmesh agent to move my player object to the clicked location on the screen.
Has anyone come up with a solution for this?
I use the new UI on Android and it reacts to touch absolutely fine. You've not got a CanvasGroup on there that's had blocksRaycast switched off have you?
Don't think Navmesh is the way to go if using the UI but probably need more info.
For reference I use a Samsung Galaxy S4 for testing.
EDIT
O$$anonymous$$ GraphicsRaycaster isn't used on my UI, can you test with pointer events. I can't think of a reason why pointer events wouldn't work for you but then there's a world of different ways of doing things with Unity.
The new UI works great but my raycasts pass through it to the gameobjects below, the problem is I want the UI system to block the raycast and it doesn't.
I've got the canvas group with blocksRaycast switched on in an attempt to stop it from passing through to the gamescene below.
I should note I am using the GraphicsRaycaster attached to the canvas, I've also tried moving it to the main camera to no avail.
I have the same issue with Unity 5... It doesn't block raycasts when I run my application on Android but works perfectly on computer... please help us if someone find a solution because it's really annoying for the development of my game.
Answer by Piflik · Apr 09, 2015 at 10:45 AM
You can activate the standalone Input Module on your EvenSystem on Mobile Devices. If you do this you might want to deactivate the Touch Input Module.
This will make "EventSystem.current.IsPointerOverGameObject()" work correctly.
(I haven't tested extensively though, so there might be other side-effects)
any chance of expanding on this? my app has 3d objects in the world and then a canvas with buttons and as stated with others the taps on mobile pass through the button and are picked up by the game objects trigger colliders.
What I dont get is I have the collider check present in the update function and it handles multiple collisions via game object name to fire specific code.
I have the button code in a separate script and set as on click in the editor.
How do I manager this scenario?
Thanks
The Event System has two Input $$anonymous$$odules attached: Standalone Input module and Touch Input $$anonymous$$odule. Both have a checkbox to "Force [the] $$anonymous$$odule Active". If you check this box for the Standalone Input $$anonymous$$odule, this is active on mobile (and vice versa if you check it for the Touch Input $$anonymous$$odule), and "EventSystem.current.IsPointerOverGameObject()" works correctly (at least in the version of Unity I used at te time of writing my answer to the question).
As I said I didn't test it enough to ascertain if disabling the Touch Input $$anonymous$$odule in favour of the Standalone Input $$anonymous$$odule has adverse side-effects on mobile devices, though.
Here is the Unity Doc page on how to use this to filter mouseclicks on UI Elements.
Thanks for the feedback, I will look the link you posted.
If I could just ask if you could clarify..
So for mobile I should be forcing Touch module to be active rather than standalone?
EventSystem.current.IsPointerOverGameObject() confuses me as code I see says !EventSystem.current.IsPointerOverGameObject() meaning to me if the UI button is not over a game object its code will run but if if is it wont? But I want the UI Button code to run whenerver it is tapped regardless of over a game object.
The game object code should only run when it is tapped directly as in full view and not beneath a UI object...
Thanks
"EventSystem.current.IsPointerOverGameObject()" seems to only work with the Standalone Input $$anonymous$$odule, so you should force this active.
The function name is a bit missleading, but GameObject in this context refers to UI Element. So you can doe Game Logic, like raycasts to the mouse position and similar, if that function returns false.
I have tried this :
if (Input.touchCount > 0) {
if (!EventSystem.current.IsPointerOverGameObject ()) {
Debug.Log ("Did not Click on the UI");
Touch userTouch = Input.GetTouch (0);
Ray ray = Camera.main.ScreenPointToRay (userTouch.position);
RaycastHit hit;
if (Physics.Raycast (ray, out hit)) {
if (hit.collider.gameObject.name.Substring (0, 6) == "Danashe") {
Open$$anonymous$$emberPage (hit.collider.gameObject.name);
} else if (hit.collider.gameObject.name == "$$anonymous$$andate") {
// code here
} else if (hit.collider.gameObject.name == "Forged") {
// code here
}
}
}
}
I force enabled standalone...and this works...penny has dropped! Thanks for the help ;-)
Answer by Haze-Games · Mar 16, 2016 at 05:46 PM
Hey guys,
I imagine you have found a solution. I had the same problem and what saved it for me is the following:
The EventSystem.current.IsPointerOverGameObject()
function takes an ID
for which pointer should be tested. 1
is the first finger, 2
is the second finger, etc.
By default, if you don't specify any argument, it uses the mouse left click ID (-1), and doesn't work on mobile devices ;)
I hope this helps.
Cheers, Charles, from Haze Games.
oddly I now find neither way is working for me and cannot see why?
Ensuring standalone is forced and then using either:
if (!EventSystem.current.IsPointerOverGameObject ()) { ...}
or
if (!EventSystem.current.IsPointerOverGameObject (touch.fingerId)) {...)
does not stop UI button taps passing through to underlying 3D objects?
Your answer
![](https://koobas.hobune.stream/wayback/20220613182728im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Correct use of transform.position of UI element into game screen 1 Answer
Fading Out UI On Play 1 Answer
How to get UI element into HMD Unity 5.3.4p1 - Oculus Utilities 1.3 0 Answers
Create UI raycast like a mouse Click 2 Answers
Is there a better way to access the single active Toggle in a ToggleGroup? 4 Answers