2
\$\begingroup\$

Is there any way to use UI-Based event triggers on Non-UI objects?

Example : on any UI object, you add an EventTrigger component, set the event, and then set the actions for the event. Its easy, and clean, to make UI do various actions on things like mouse enter / exit, drag, etc.

For non UI objects (a cube, for example) these kind of things are mostly done through scripts with raycast code. Its not impossible, but I find it messier, harder to organize, and less flexible then the EventTrigger approach.

I'm working on a turn based strategy type game. Much of the game mechanics is based around mouse movement and clicking. I tried just adding an on-click event to a cube, but it didn't do anything.

Is this scenario just plain not supported?

\$\endgroup\$
  • \$\begingroup\$ Presumably you tried firing your event in OnMouseDown? \$\endgroup\$ – DMGregory Sep 2 at 20:33
  • \$\begingroup\$ i assume not. but you can simply simulate them on non-UI objects. they have their own Callbacks \$\endgroup\$ – virtouso Sep 2 at 21:13
  • \$\begingroup\$ I realize there are events on Monobehavior like OnMouseDown, OnMouseOver, etc. My problem with these is that the 'when' and 'what' get coupled together. I imagine its possible to uncouple these things. But if it would instead just work with the event-triggers, I wouldn't be re-inventing the wheel. \$\endgroup\$ – Sperry Sep 2 at 23:05
2
\$\begingroup\$

You can do this if you add a Physics Raycaster component to your camera. Just make sure you have colliders on the same GameObjects that have the EventTriggers you want to be clickable.

You also have the option to do this with 2D physics using a Physics 2D Raycaster.

\$\endgroup\$
  • \$\begingroup\$ This worked perfect. Thanks! \$\endgroup\$ – Sperry Sep 3 at 2:41

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.