UI Button OnClick Listener not triggering

End goal: Make clickable text answers from my list of possible answers.

At first all I had was a list of UI Text prefabs and I tried the slightly more complicated way of setting a EventTrigger to each one with all the poutine another unityAnswers answer described.

When that didn’t work and I didn’t know why, I figured maybe making a Button prefab with a Text Component would allow me the simpler task of just setting the OnClick.

Alas, no errors, but nothing at all triggers. Here’s my code (I’ll cut irrelevant pieces out for readability)

If anyone could be so kind as to let me know what’s missing or preventing my OnClicks from firing, I could finally (temporarily) stop banging my head against my desk!

		//Recreate for this StoryNode
		for (int index = 0; index < answers.Length; index++) {

			DeserializedAnswers.Answer answer = answers[index];

			if(CheckAnswerCondition(answer.condition, answer.amount)){

				GameObject text = Instantiate (answer_text_prefab, lastPosition, canvasTrans.rotation) as GameObject;

				Button b = text.GetComponent<Button>();
				buttonSetup(b);

			}}


	void buttonSetup(Button button) {
		//Remove the existing events
		button.onClick.RemoveAllListeners();
		//Add your new event using lambda notation
		button.onClick.AddListener (handleButton);
	}

void handleButton() {
Debug.Log(“Button pressed!”);
}

I don’t know what you were trying with all your code so I had to make some things up but I can create buttons with this and it returns the button name with the index as well.

	void CreateButtons()
	{
		//Recreate for this StoryNode
		for (int index = 0; index < answers.Count; index++) {
			Answers MyAnswer = answers[index];
			{
				GameObject text = Instantiate (answer_text_prefab, Vector3.one, canvasTrans.rotation) as GameObject;
				text.transform.SetParent(canvasTrans, false);
				
				Button b = text.GetComponent<Button>();
				b.name = "Button " + index.ToString ();
				buttonSetup(b);
			}
		}
	}
	
	void buttonSetup(Button button) {
		//Remove the existing events
		button.onClick.RemoveAllListeners();
		//Add your new event using lambda notation
		string myName = button.name.ToString ();
		button.onClick.AddListener (() => handleButton(myName));
	}
	
	void handleButton(string MyName) { Debug.Log("Button "+ MyName + " pressed!"); }

Of course you could just have a panel over the top of your buttons as well.

Let me know if you get any better results from my code.

PS I used List so Count instead of Length in that for loop and I wasn’t sure about the lastPosition so I just used a vertical layout group and set parent to the panel that was on.

Thanks for the help, Mmmpies! (and happy belated birthday, yesterday?.. ;))

So I ended up finding out that:

Having the event system as a component of my Canvas prefab was useless.

Still not sure why but by creating a Event System in my inspector (not as a child of canvas), my buttons were finally “listening” and calling their callbacks.

I wanted to “recall” my question so no one would waste their time but I couldn’t see it in my profile, only the other question I’d asked a week ago.

“I don’t know what you were trying with all your code” → Yeah, I tried to cut it down to the necessary bits but maybe I should include lots of comments.

Thanks for taking the time!