- Home /
Button doesn't called OnPointerDown() method
So I decided to learn Unity by modifying Pacman game tutorial on desktop become Android game. For now, I tried to replace the input key from arrow key to UI button on my sprite script. And then drag the button as to the sprite input field.
Bellow code supposed to be execute when I pressed my button. However, the button seems doesnt trigger any method.
if (rightButton.IsInvoking("OnPointerDown")){
Debug.Log("OnPOinterDOwn() method is called");
Here is the complete code for my sprite :
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class PacmanMove : MonoBehaviour {
public float speed = 0.4f;
Vector2 dest = Vector2.zero;
public Button rightButton;
public Button leftButton;
public Button upButton;
public Button downButton;
// Use this for initialization
void Start () {
dest = transform.position;
}
//Update is called in fixed time interval
void FixedUpdate() {
//Move closer to Desstination
Vector2 p = Vector2.MoveTowards(transform.position, dest, speed);
GetComponent<Rigidbody2D>().MovePosition(p);
//Check for input if not moving
if ((Vector2)transform.position == dest) {
if (Input.GetKey(KeyCode.UpArrow) && valid(Vector2.up))
dest = (Vector2)transform.position + Vector2.up;
if (Input.GetKey(KeyCode.RightArrow) && valid(Vector2.right))
dest = (Vector2)transform.position + Vector2.right;
if (Input.GetKey(KeyCode.DownArrow) && valid(-Vector2.up))
dest = (Vector2)transform.position - Vector2.up;
if (Input.GetKey(KeyCode.LeftArrow) && valid(-Vector2.right))
dest = (Vector2)transform.position - Vector2.right;
//Bellow code supposed to be trigger when my UI button is pressed
if (rightButton.IsInvoking("OnPointerDown")){
Debug.Log("OnPointerMethod() is called");
}
}
// Animation Parameters
Vector2 dir = dest - (Vector2)transform.position;
GetComponent<Animator>().SetFloat("DirX", dir.x);
GetComponent<Animator>().SetFloat("DirY", dir.y);
}
bool valid (Vector2 dir) {
//Cast line from 'next to Pac-Man' to 'Pac-man'
Vector2 pos = transform.position;
RaycastHit2D hit = Physics2D.Linecast(pos + dir, pos);
return (hit.collider == GetComponent<Collider2D>());
}
}
Could somebody enlighten me what am I missing?
Answer by metalted · Jun 21, 2019 at 03:13 PM
So as I understand the invoking, there is a method called Invoke() and a method IsInvoking(). When calling the Invoke() you enter the function and the waiting time as the parameters. The IsInvoking() method checks if the specified method is on the "waiting list". In line 38 you are checking if the method OnPointerDown "IsInvoking". In other words, is the method on the waiting list to be executed. You never actually Invoke the method "OnPointerDown", not in this script anyway, so this If statement will always be false. That is why you will not get the Debug.Log(message). So make sure you are actually invoking the OnPointerDown method.
Your answer

Follow this Question
Related Questions
Screen scaling causing text to be outside button 0 Answers
All UI buttons appear and stack on top of each other. 1 Answer
Unity - destroy clone child object (button) 1 Answer
[5.4 - Unity2D - Android] UI Button Requires Child Text? 0 Answers
My variable value doesnt change through a function called by a button? 1 Answer