Hello guys,
I’m trying to add an onClick in code to a canvas UI button and i’ve tried 3 different approaches and none of them are working. Any suggestions on how to make this work?
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
using System.Collections;
using TriviaQuizGame;
using TriviaQuizGame.Types;
public class OnClick_Button_Canvas : MonoBehaviour
{
public Button buttonPrefab; //default button
//private TQGGameController tqgameController;
private TQGGameController tqgameController2;
private int index = 0;
public void start()
{
GameObject go = GameObject.Find("GameController");
//TQGGameController tqgameController = (TQGGameController)go.GetComponent(typeof(TQGGameController));
TQGGameController tqgameController2 = new TQGGameController();
//TRIED 3 DIFFERERENT APPROACHES:
//buttonPrefab.onClick.AddListener(delegate { MyMethod(0); });
//buttonPrefab.GetComponent<Button>().onClick.AddListener(() => { MyMethod(index);});
buttonPrefab.onClick.AddListener(MyMethod(0));
}
public void MyMethod(int index)
{
Debug.Log("Button Clicked");
tqgameController2.ChooseAnswer(index);
}
}