- Home /
button.onClick.Addlistener not working for touch
Hi guys!
I'm sorry to bother you with such a basic question, but I've searched and experimented for hours and can't figure this one out.
I've got a bunch of buttons, each has the script below attached. Clicking the button will trigger "DoAction", which finds the player (through a static reference) and runs the "Action" method of the player.
This works fine for mouse input, but when I attach my phone through UnityRemote it does nothing. From what I understand, my code should catch Touch events as if they were Clicks, so I'm confused as to why it doesn't work.
Interestingly, Input.OnMouseDown inside of Update() does work with my phone with UnityRemote (but I'd rather not use this approach to avoid attaching colliders for raycasting).
So, does anyone know what is going on? Any ideas to solve this problem? Thank you very much for your time!
protected Button button;
protected string text;
void Awake()
{
SetReferences();
}
protected virtual void SetReferences()
{
button = this.gameObject.GetComponent<Button>();
text = this.gameObject.GetComponentInChildren<Text>().text;
button.onClick.AddListener(() => { DoAction(text); });
}
private void DoAction(string text)
{
Player.Reference.Action(text);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613184408im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
GUITexture OnMouseDown Problem 1 Answer
Input.getTouch() vs checking if button is clicked 1 Answer
Detect UI Button Click Event in Update method 3 Answers
GUI Button touch problem 1 Answer
GUI Repeatbutton up? 1 Answer