Right now I am currently using a class to populate the cells of a grid layout with buttons onto a scroll view. The buttons have the name of the quest and are populating the gridlayout as I wanted. The problem I am having is when I add a button to the UI, I am trying to add a function via code that happens when I click that button. Upon doing the “StartQuest” function is when I add the quest button to the ui using another classes function Populate() which literally just instantiates the gameobject to the scroll view.
public void StartQuest()
{
questActive = true;
Button uiButton = questUIForGrid.GetComponent<Button>();
questNameUI = uiButton.GetComponentInChildren<Text>();
questNameUI.text = questName;
//uiButton.onClick.AddListener(SetDescription);
GameObject thisQuestUI = uiButton.gameObject;
thisQuestUI.GetComponent<Button>().onClick.AddListener(SetDescription);
popQuestLog.Populate(thisQuestUI);
}
The function I am trying to add to the button is the SetDescription, where I would simply activate another UI image with a text element as the child making this “appear” with a description of whatever quest you’re on. I’m getting no nullreferences and assume this is just due to my ignorance of UI. PLEASE HELP! and let me know if you need more info, Thanks!
public void SetDescription()
{
qMan.descriptionImage.SetActive(true);
qMan.descriptionText.text = questDescription;
}
EDIT The main problem is the function isn’t being added to the button click, although the quest name is working fine and I got most of the logic from the docs Unity - Scripting API: UI.Button.onClick