I’m working on a menu system using canvas UI stuff, and I’m having a strange issue with the OnClick functions for Buttons.
My current setup, I have buttons with an OnClick function, OpenMenu(GameObject), with each one given a specific object that contains buttons for other parts of the menu (for example the options screen or the level select screen.) Each of these menus have a Back button which opens the Main menu object.
All of this works fine, but I wanted the Escape key to also return to the Main menu. The issue I am having is that after pressing the escape key to return to the main menu (which works as expected), the next time I click a button, the OpenMenu function on the OnClick event is receiving the GameObject from the previous click instead of the object attached to the OnClick event of the actual button I clicked.
So, for example, if I click Options and then press Escape instead of clicking Back, and then click Level Select, the Options window will open again. I’ve confirmed in the VS2015 debugger that the OpenMenu function is actually receiving the Options object instead of the LevelSelect object I would expect in this case.
My OnClick function is as follows:
public void OpenMenu( GameObject menu )
{
m_activeMenu.SetActive(false);
m_activeMenu = menu;
m_activeMenu.SetActive(true);
}
and pressing escape simple calls OpenMenu(m_mainMenu);
Both m_mainMenu and m_activeMenu are GameObjects that serve as parents to the other UI elements on those menus but are otherwise empty themselves. The button OnClick events are setup in the editor (see screenshot), and not dynamically generated at runtime.
I really have no idea what’s going on here - as I said, the debugger is definitely showing that OpenMenu is getting the previous GameObject rather than the one that should be provided by the button clicked, so I’m pretty stumped. If anyone has seen anything like this or has any idea what’s going on, help is greatly appreciated.
Thanks!