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 codeusing 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
-
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 descriptionHowever, 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.
Related articles
- c# - unity vector3+vector3 can be done, but vector3/vector3 cannot be done
- c# - cannot get component in unity
- unity error "cannot place camera with tag "maincamera" in the world"
- c# - if you warp with unity transformposition, it will blow away at the warp destination
- c# - is it possible to execute the same name method of multiple classes?
- c# - the ball does not roll on the floor tilted during the game in unity
- c# - about random generation of unity objects and setting of initial position
- unity c# list is heavy
- c# - [unity] [photon] serialization of classes including list>
- c# - how to use unity initialization code
- c # - unity) how to convert the path passed to resourcesload ()
- c# - i want to change the animation of mecanim in unity
- c# - retaining the content of collisions in unity
- c# - unity skybox stencil buffer
- c# - [unity] in phton, i am in trouble because i get an error and cannot get out of the room
- c# - unity) specify the size with camera and save
- c# - [unity] i want to output the log to the game screen as well, but when the scene transitions, the log cannot be output
- c# - about method search
- c# - [unity] processing when the loop is divided into two is heavy
- c# - unity i want to quickly generate random items from a fixed range
- python - you may need to restart the kernel to use updated packages error
- dart - flutter: the instance member'stars' can't be accessed in an initializer error
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- javascript - how to check if an element exists in puppeteer
- sh - 'apt-get' is not recognized as an internal or external command, operable program or batch file
- i want to check the type of a shell script variable
- i want to call a child component method from a parent in vuejs
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.