Canvas Element - Scales with screen size, but not clickable (2D)

Hi all

I am working through a simple Unity game and have been through quite a few tutorials.

  1. I have a Canvas element with children items
  2. I added game objects via GameObject → UI → Panel;
  3. Components of these objects are: Rect Transform; Canvas Renderer; Image (Script); My Script attached as a component
  4. These items show up in Game preview pane in the correct place, and scale appropriately when I change resolution

Problem: When I run my game at resolution 1 (750x1334) the items work as intended executing my script [ a basic onMouseDown() ], but when I run at resolution 2 (375x667) the onMouseDown doesn’t fire.

Can anyone point me in the right direction to fix this? I’m struggling to debug it since it works on resolution 1 but not 2.

Thanks

If you are gonna use UI elements, On Mouse Down might not be the best idea to catch button presses. You should be using a Button UI element (which is the same as an Image element with the button component added). Then you would execute your method by adding it to the button script’s list of On Click methods. This is how you normally handle button presses. Also AFAIK, at least for colliders, On Mouse Down performance is significantly slower than a normal UI button press since it is a message sent to every possible element that can and should catch the message. So you should avoid it when possible.

As for the reason of your On Mouse Down not firing when running at a different resolution, sometimes some UI elements can end up on top of each other blocking clicks from passing through, if that’s what’s happening you may solve the issue by ticking off the Raycast Target field on images and other UI elements that don’t need to catch button presses.

Hope this helps!

@MacDx - thanks for the reply. I couldn’t get it to work using the Raycast fix you suggested, but I did go back through the UI tutorial and reworked my interface to use the UI buttons and the OnClick event system. Using this I have it working for now, and hopefully this is more in keeping with best practice.