Search Unity

Resolved Detect click on game object with "Invoke Unity Events" option?

Discussion in 'Input System' started by Selek-of-Vulcan, Sep 28, 2022.

  1. Selek-of-Vulcan

    Selek-of-Vulcan

    Joined:
    Sep 23, 2021
    Posts:
    78
    I am able to detect a click on a cloned game object if I use the "Send Messages" option. I attach a Physics Raycaster 2D to my camera, implement an IPointerHandler, and it works great. But this method detects two events -- started and performed -- rather than just the one I need. And I can't find a way using eventData to weed out "started" from "performed" clicks.

    I see that CallbackContext (used with the "Invoke Unity Events" option) *can* distinguish between those two types of events. Moreover, in general I'd prefer to use the "Invoke Unity Events" option, as it seems more flexible.

    But I can't get the "Invoke Unity Events" option to properly detect a click on a cloned game object. I can get the mousePosition easily enough, but then I have to do cumbersome math to figure out which game object is being clicked. The CallbackContext parameter returns the name of my generic prefab, not the specific name of THIS instantiated game object, even if I use this.name. Likewise, the parameter returns integer variables always set to 0, not the value held in the prefab object. Is there a way to do this using "Invoke Unity Events", or am I stuck using "Send Messages", which sends two click events?
     
    Last edited: Sep 28, 2022
  2. Selek-of-Vulcan

    Selek-of-Vulcan

    Joined:
    Sep 23, 2021
    Posts:
    78
    I answered my own question. If you use the "Invoke Unity Events" option, then you have to raycast yourself once the event is triggered. I added a 2D raycast to the method called by the event, and presto, it worked.

    By contrast, if you use the "Send Messages" option, that requires you to set up a Physics2D raycaster on your camera, and I guess the messages system automatically does the raycast for you. I'm not sure why the "Invoke Unity Events" option doesn't partake in this functionality, but I can live with it.