Trying to create "responsive" quest log

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

If uiButton is already a Button, why are you doing .gameObject.GetComponent()?

I don’t see anything obviously broken in your instantiation code. Try adding a Debug.Log message to SetDescription so you can verify whether it is failing to run or if it’s running but doing the wrong thing. Also, make sure you are adding the SetDescription function from the correct object (when you just write “SetDescription”, it means “this.SetDescription”).

@Antistone Because I was getting multiple buttons that would always change to the most recent Text as opposed to getting a new “instance” of a button per active quest that would contain that particular quests name on the UI, if that makes sense. I’m just testing for now so I’m sure that can be improved as well, but it will be chicken scratch for a while until I solve the main issue but the instantiating isn’t the problem of the actual GameObject (button, that’s working fine)

Instead, It’s mainly the function to the button issue, and that is the logic I wanted to use (this.SetDescription), so i’m assuming (since the class this is in is named Quest, and monobehavior inherit if that matters, doubt it) I should make an new instance of a Quest as well and assign the button to that (not around my pc to test)