I have a gameobject inside which there are several sprites. I need to make this gameobject clickable (when clicked, the function is called with parameters).

buttons.GetComponent<Button>().onClick.AddListener(() => load_level(level_name_, location_name_)); 

How to write down a similar entry only for gm,

for such a record:

  buttons.GetComponent<EventTrigger>().OnPointerEnter(() => load_level(level_name_, location_name_)); 

does not work

Upd:

I have an array of the gameobject and list with a set of data different for each gameobject, in a loop I pass this data into gameobjects. Therefore, I need to bind a function call with parameters from lista right away.

    2 answers 2

    First method: (If you still want your own method)

     void Start() { EventTrigger pointerHoverTrigger = buttons.GetComponent<EventTrigger>(); EventTrigger.Entry yourNewEntry = new EventTrigger.Entry(); yourNewEntry.eventID = EventTriggerType.PointerClick; pointerHoverTrigger.triggers.Add(yourNewEntry); yourNewEntry.callback.AddListener((eventData) => { //... print("Нажата ЛКМ"); }); } 

    Second way : (If you need to catch mouse clicks (using the OnMouseOver method))

    1. Add a collider to the object. This will be the area you need to click.
    2. Add the void OnMouseOver() method
    3. Check the click on the object: if (Input.GetMouseButton(0)) // 0 - LKM, 1 - PCM, 2- SCM.

    Those. You should get the following code:

     void OnMouseOver() { if (Input.GetMouseButton(0)) { Debug.Log("Нажата ЛКМ"); } } 
    • On the first point: Object reference not set. I don’t understand why. googled on the line on the unity answers the same example and everything plows ... - Slimper
    • @Slimper Something, probably, with a link. Check if you are really referring to that GO. And does the EventTrigger hang on on GO EventTrigger - Pepsi4

    The easiest way is to add a collider to GameObject and implement the methods MonoBehaviour on OnMouseDown, OnMouseDrag, OnMouseEnter, OnMouseExit, OnMouseOver, OnMouseUp

     public class Hello : MonoBehaviour { void OnMouseDown(){ Debug.Log("Mouse click catched!"); } }