Button not working with delegate

I have been following “GameGrind’s” tutorial on yt, yet it seems i have a problem with one that may seem simple yet i have tried everything. I can not get the button to work.

I debugged it, Changed from button click to a mouseclick(as can be seen in code), it works, all functions work as intended. The only issue is when i try to use a delegate to make it work. when i click the button nothing happens, and i have no idea why.

I simply want the ContinueDialogue(); to run when i click the button.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


public class DialogueSystem : MonoBehaviour {

   
    public static DialogueSystem Instance { get; set; }
    public List<string> dialogueLines = new List<string>();
    public string npcName;
    public GameObject dialoguePanel;

    Button continueButton;
    Text dialogueText, nameText;
    int dialogueIndex;

    // Use this for initialization

    void BtnPressed()
    {
        Debug.Log("button has been pressed");
    }
    void Awake () {

        continueButton = dialoguePanel.transform.FindChild("Continue").GetComponent<Button>();
        dialogueText = dialoguePanel.transform.FindChild("Text").GetComponent<Text>();
        nameText = dialoguePanel.transform.FindChild("Name").GetChild(0).GetComponent<Text>();

        //continueButton = dialoguePanel.transform.FindChild("Continue").GetComponent<Button>();
        continueButton.onClick.AddListener(delegate { ContinueDialogue(); } );

        dialoguePanel.SetActive(false);


        if (Instance != null && Instance !=this )
        {
            Destroy(gameObject);
        }
        else
        {
            Instance = this;
        }
    }//awake

    public void CreateDialogue()
    {
        dialogueText.text = dialogueLines[dialogueIndex];
        nameText.text = npcName;
        dialoguePanel.SetActive(true);
    }
    public void ContinueDialogue()
    {
        if(dialogueIndex < (dialogueLines.Count -1))
        {
            dialogueIndex++;
            dialogueText.text = dialogueLines[dialogueIndex];
        }
        else
        {
            dialoguePanel.SetActive(false);
        }
    }

    public void AddNewDialogue(string[] lines, string npcName)
    {
        dialogueIndex = 0;
        dialogueLines = new List<string>(lines.Length);
        dialogueLines.AddRange(lines);
        Debug.Log(dialogueLines.Count);
        this.npcName = npcName;
        CreateDialogue();
    }
   
   
    // Update is called once per frame
    void Update () {
    if(Input.GetMouseButtonDown(1)){
        //dialoguePanel.SetActive(false);
        ContinueDialogue();
    }
       
    }
}

Try making the Awake() be a Start()… there was some quirkiness in terms of the Buttons in a scene not fully existing at the time you go looking for them in Awake(), but they will certainly be there by the time Start() happens.

To elaborate a little bit on “quirkiness” … :slight_smile:

So, all of your scripts will execute Awake() and then all of your scripts will execute Start().

Awake is a good place to setup any references for components on the same object, and Start() is a good place to make connections to other game objects (and/or their components).

If A loads before B, you cannot look up a component on B inside A (in Awake). for example.

2 Likes

already tried changing awake() into start(), that does not work. On the inspector under OnClick() i tried adding the object with the script and selecting the function like so, that doesnt work either. The button is not highlighting either, completely ignoring any mouse input.

I add listeners to UnityEvents like this

button.onClick.AddListener(Method);

Any reason why it can’t be done like that?

I tried doing so, nothing changes. Cant tell exactly why its done this way as im still learning, but it doesnt work for sure. i also tried doing this. The guy is making the whole setup of the button thru code.

continueButton = dialoguePanel.transform.FindChild("Continue").GetComponent<Button>().onClick.AddListener(ContinueDialogue);

but it says can not convert from type void to unity.ui

i notice in your screenshot, your image component’s “Raycast Target” checkbox is unticked. That property indicates whether the UI element detects the cursor or not. Turn it on and it should resolve the problem.

Also make sure you’ve got an EventSystem in your scene (appears to be missing, unless you’ve renamed the object or moved it into another hierarchy). It’s essential for anything related to the “built-in” UI interaction

1 Like

indeed i was switching it on an off, i literally tried everything.

Also, i managed to fix it. i did this like 3 times before but this time it worked, i deleted the whole canvas, redid the whole thing under different names. I got the button to work in another project using the same method and setup and now it works ;d must have been some bug in my project or something.

Ah, finally… Glad you got this working :slight_smile: