Unity onClick.addlistener not working

梦想的初衷 提交于 2019-12-12 02:31:41

问题


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 heirarchy. I also opened up the part of the code that attaches buttons to the listener for debugging. I dont 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;
    }
}

回答1:


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




回答2:


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. :)




回答3:


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.



来源:https://stackoverflow.com/questions/37797071/unity-onclick-addlistener-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!