I’m trying to add an onClick listener to a game object’s button component. It gets added successfully, but when I click the game object, nothing happens.
Here is a picture of my inspector (was not able to attach an image):
I instantiate all of the game objects at the beginning of the build then apply the listeners, but I think the issue has to do with the game object and registering the click.
UnityEngine.Events.UnityAction bomb_holder_action;
void Start()
{
bomb_holder_action = () => { AddBomb(); };
bombs = 0;
AddBomb();
int i = 0;
foreach (GameObject go in GameObject.FindGameObjectsWithTag("FlowObject_Bomb"))
{
go.GetComponent<Button>().onClick.AddListener(bomb_holder_action);
Debug.Log("Listener added to bomb#" + i);
i++;
}
}
Some of the possible issues I see might be that the button component doesn’t have a canvas and is missing a target graphic. I’m not sure how I would implement a canvas since the game objects move and get destroyed after some time. When I add a target graphic through an image game object, it doesn’t move with the parent object. If anyone can help, it would be appreciated. I’ve been messing around with this for a few hours to no avail.