Kaizad Avari
Kaizad Avari

Reputation: 11

Why don't my button listeners work in Unity?

I load a Canvas prefab at runtime when an event occurs. The canvas simply has a Panel inside it, which in turn has 2 buttons. I'm trying to add OnClick events to both these buttons in the script, but it only works for the first button somehow! I have the following two lines of code one after the other:

GameObject.Find("RestartButton").GetComponent<Button>().onClick.AddListener(() => RestartClicked());

GameObject.Find("ViewButton").GetComponent<Button>().onClick.AddListener(() => ViewClicked());

The callback only works for the RestartButton, and not for the ViewButton. It may well be a very small thing, but I searched Google and Bing extensively and remain clueless, so any help would be appreciated.

Thanks!

Edit:

The script instantiates the prefab and tries to reference the two buttons in the prefab through GameObject.Find(), given that once Instantiate is called the Canvas should be active in the hierarchy. I also opened up the part of the code that attaches buttons to the listener for debugging. I don't think it attaches the listener at all.

void Start () {

    SetupScene();

    var prefab = Resources.Load("RestartOrViewPrefab");
    if (prefab == null)
    {
        Debug.LogAssertion("Prefab missing!");
        return;
    }
    restartCanvas = (GameObject)Instantiate(prefab);
    Button btn = GameObject.Find("ViewButton").GetComponent<Button>();
    btn.onClick.RemoveAllListeners();
    btn.onClick.AddListener(() => ViewToggle());
    Button btn2 = GameObject.Find("RestartButton").GetComponent<Button>();
    btn2.onClick.AddListener(() => RestartClicked());
}

private void RestartClicked()
{
    Debug.Log("RESTARTING");
    SetupScene();
}

private void ViewToggle()
{
    Debug.Log("TOGGLE");
    if (freshStart)
    {
        //Something here
        freshStart = false;
    }
    else
    {
        //Something else here
        freshStart = true;
    }
}

Upvotes: 1

Views: 14999

Answers (3)

brosilio
brosilio

Reputation: 5

I answered this on another post, but I'll answer it here for people that still need this info. The GameObject your instantiated button is a child of must have a CanvasRenderer component on it. I'm not sure why this works, but it does. Once the parent of the button GameObject has the CanvasRenderer on it, you can call myButton.onClick.AddListener(() => MyMethod(MyArgs)); as you normally would.

Upvotes: 0

Kaizad Avari
Kaizad Avari

Reputation: 11

So I took two days to solve my problem. Apparently, doing a GameObject.Find() for a GameObject within a prefab that you just instantiated doesn't work. We always need to use the GameObject that the Instantiate() method returns to find any component within the prefab. The code I used to make my scene work may not be the best solution if your prefab is very big/complex (say you have 15 buttons and need to do something with one), but it sure does work. :) I replaced the following lines of code:

Button btn = GameObject.Find("ViewButton").GetComponent<Button>();
btn.onClick.RemoveAllListeners();
btn.onClick.AddListener(() => ViewToggle());
Button btn2 = GameObject.Find("RestartButton").GetComponent<Button>();
btn2.onClick.AddListener(() => RestartClicked());

With the following lines of code:

Button[] buttons = restartCanvas.GetComponentsInChildren<Button>();

    foreach(Button but in buttons)
    {
        if(but.gameObject.name == "RestartButton")
            but.onClick.AddListener(() => RestartClicked());
        else if(but.gameObject.name == "ViewButton")
            but.onClick.AddListener(() => ViewToggle());
    }

So I just reused the restartCanvas that I got a reference to.

restartCanvas = (GameObject)Instantiate(prefab);

Hope this helps someone. :)

Upvotes: 0

LumbusterTick
LumbusterTick

Reputation: 1075

Ok make sure you are not getting any null reference error, and check tht spelling of you buttons , output some debug logs in your second function to see if its even reaching there

Upvotes: -2

Related Questions