Home>
The problem i am experiencing

As the title says, I want to add EventTrigger to the object from the script and register the method with AddListener to the attached EventTrigger, but I can not do it.

What I confirmed is that the processing has passed to the end and the EventType can be specified, but the execution content was not registered.
I also confirmed that the Event System exists on the Scene, I tried it on a new project, but it seems that it is not a project-specific problem because I could not register. No error message has occurred.

Since EventTrigger was added on the Editor and the method could be specified manually, it does not mean that the method cannot be accessed.

Applicable source code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class TapIconToChangePanel :GetParentChilds
{
    public GameObject[] Parents;
    private string oldSelectIcon = default;
    public override void GetChild()
    {
        Parent = Parents;
        base.GetChild();
    }
    void Awake()
    {
        GetChild();

        for(int x = 0;x<Panels.Count;x++)
        {
            for(int i = 0;i<Panels[x].Count;i++)
            {
                if(x == 0)
                {
                    Panels[x][i].GetComponent<Image>().color = new Color(1, 1, 1, 0.4f);
                    EventTrigger currentTrigger = Panels[x][i].AddComponent<EventTrigger>();
                    EventTrigger.Entry entry = new EventTrigger.Entry();
                    entry.eventID = EventTriggerType.PointerDown;
                    entry.callback.AddListener( (date) =>{CloseAndOpenPanel();} );// The right side of the lambda expression is the method to add.
                    currentTrigger.triggers.Add(entry);
                }
                if(x == 1)
                Panels[x][i].SetActive(false);
            }
        }
        Panels[0][1].GetComponent<Image>().color = new Color(1, 1, 1, 1);
        Panels[0][1].SetActive(true);
    }
    void Update()
    {
    }
    public void CloseAndOpenPanel()
    {
        if(oldSelectIcon == this.gameObject.name)
            return;
        oldSelectIcon = this.gameObject.name;
        for(int x = 0;x<Panels.Count;x++)
        {
            for(int i = 0;i<Panels[x].Count;i++)
            {
                if(x == 0)
                    Panels[x][i].GetComponent<Image>().color = new Color(1, 1, 1, 0.4f);
                if(x == 1)
                {
                    if(Panels[x][i].name == oldSelectIcon)
                        Panels[x][i].SetActive(true);
                    else
                        Panels[x][i].SetActive(false);
                }
            }
        }
        this.gameObject.GetComponent<Image>().color = new Color(1, 1, 1, 1);
    }
}
  • Answer # 1

    The execution content was not registered.

    What do you think about this "unregistered" state, and what did you think it was, but please clarify that it was like this. Because I don't know what to look at for the "unregistered" problem and make that decision.

    After that, you should try the simple one. There's too much code in the code to ask questions. For example, you should try from this level.

    using UnityEngine;
    using UnityEngine.EventSystems;
    [RequireComponent(typeof(RectTransform))]
    public class EventTriggerController: MonoBehaviour
    {
        void Start()
        {
            var trigger = gameObject.AddComponent<EventTrigger>();
            EventTrigger.Entry entry1 = new EventTrigger.Entry();
            entry1.eventID = EventTriggerType.PointerDown;
            entry1.callback.AddListener(call =>{Debug.Log("Pointer down.");});
            trigger.triggers.Add(entry1);
            EventTrigger.Entry entry2 = new EventTrigger.Entry();
            entry2.eventID = EventTriggerType.PointerUp;
            entry2.callback.AddListener(call =>{Debug.Log("Pointer up.");});
            trigger.triggers.Add(entry2);
        }
    }

  • Answer # 2

    First of all, I made a big mistake.
    If you manually attach an EventTrigger to an object and register an event, the Inspector will show the original object, the script to execute, and the method in it.
    Image description

    However, if an event is added to the EventTrigger from the script with AddLister, the event contents registered from the Inspector cannot be confirmed and the display of "List is Empty" remains. (The information that it is not displayed when added with AddLister is hard to find, and I accidentally found it in an article on a personal blog. Thank you for taking this opportunity.)
    Image description
    However, since the event itself is registered correctly, it is possible to execute processing according to the command of the method.
    In my case, this kind of misunderstanding was made because it was not possible to confirm whether the method of the registered method was executed. As bboydaisuke answered, it is best to use what is executed when testing.