Skip to main content
added markdown to code in text, removed unityscript tag (depreciated & not applicable to this post)
Source Link
Pikalek
  • 13.4k
  • 5
  • 49
  • 54

I don't seem to be able to make a reference to a Button inside a menu prefab.

My menu prefab consists of a panel and the panel has 4 buttons.

I added

public GameObject prefabMenu;

in my code and drag and dropped the prefab in the Unity Editor to it.

When my scene is starting (Start()Start() method) I instantiate this prefab into a GameObject like this:

panelMenu = Instantiate(prefabMenu, canvas.transform);

after that I try to refer one of the buttons with this line and set an onclickonClick listener:

btnResume = panelMenu.transform.Find("BtnResume").GetComponent<Button>();
btnResume.onClick.AddListener(() => Log.I("Working"));

The log method however doesn't not get called when I click on the button (I checked and the btnResumebtnResume is not null).

I appreciate any help on this. Thank you.

I don't seem to be able to make a reference to a Button inside a menu prefab.

My menu prefab consists of a panel and the panel has 4 buttons.

I added

public GameObject prefabMenu;

in my code and drag and dropped the prefab in the Unity Editor to it.

When my scene is starting (Start() method) I instantiate this prefab into a GameObject like this:

panelMenu = Instantiate(prefabMenu, canvas.transform);

after that I try to refer one of the buttons with this line and set an onclick listener:

btnResume = panelMenu.transform.Find("BtnResume").GetComponent<Button>();
btnResume.onClick.AddListener(() => Log.I("Working"));

The log method however doesn't not get called when I click on the button (I checked and the btnResume is not null).

I appreciate any help on this. Thank you.

I don't seem to be able to make a reference to a Button inside a menu prefab.

My menu prefab consists of a panel and the panel has 4 buttons.

I added

public GameObject prefabMenu;

in my code and drag and dropped the prefab in the Unity Editor to it.

When my scene is starting (Start() method) I instantiate this prefab into a GameObject like this:

panelMenu = Instantiate(prefabMenu, canvas.transform);

after that I try to refer one of the buttons with this line and set an onClick listener:

btnResume = panelMenu.transform.Find("BtnResume").GetComponent<Button>();
btnResume.onClick.AddListener(() => Log.I("Working"));

The log method however doesn't not get called when I click on the button (I checked and the btnResume is not null).

I appreciate any help on this. Thank you.

edited tags
Link
Source Link

Button onClick in a menu prefab doesn't work

I don't seem to be able to make a reference to a Button inside a menu prefab.

My menu prefab consists of a panel and the panel has 4 buttons.

I added

public GameObject prefabMenu;

in my code and drag and dropped the prefab in the Unity Editor to it.

When my scene is starting (Start() method) I instantiate this prefab into a GameObject like this:

panelMenu = Instantiate(prefabMenu, canvas.transform);

after that I try to refer one of the buttons with this line and set an onclick listener:

btnResume = panelMenu.transform.Find("BtnResume").GetComponent<Button>();
btnResume.onClick.AddListener(() => Log.I("Working"));

The log method however doesn't not get called when I click on the button (I checked and the btnResume is not null).

I appreciate any help on this. Thank you.