I attached a button prefab reference to the another prefab of type sprite. And i am unable to call the onClick.
Below is my Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Dot : MonoBehaviour
{
public bool canBePressed;
public float speed;
[SerializeField] Button button;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.Translate(Vector2.left * speed * Time.deltaTime);
button.onClick.AddListener(Print);
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Perfect")
{
canBePressed = true;
//Debug.Log(canBePressed);
}
}
private void OnTriggerExit2D(Collider2D other)
{
if (other.tag == "Perfect")
{
canBePressed = false;
//Debug.Log(canBePressed);
}
}
void Print()
{
Debug.Log("Pressed");
}
}