OnClick event receiving GameObject from previous Click

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 = menu;

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.


As what I can see here, Is that you are using UI button, and you want to trigger some events, and from what I understood, you want to call certain gameobjects on specific button click. Something like this If I am not wrong, click on Button A call/recieve object A, and similarly onClick button B and call/recieve object B, right @Icepick ?

Looking at the image you’ve given, I don’t see any particular gameobject to call/recieve, so you might wanna provide some object related images, not mainmenu, you are having problems with objects but didn’t provide anything related to objects. So, It’s hard to see things clear.

Your objects (~sorry GameObejcts), must be identified, before calling it, so OnClick( ) should be able to know which object to call. So, The reason your OnClick( ) receiving same Game Object after Escape button, Is because Escape button Input function is not passed/set and you might not able to identify which object to call at OnClick( ).

Try to add this on your OpenMenu( ) function.

if (Input.GetKey(KeyCode.Escape))
  //Write the same code written on back UI button.

There is also an Alterative way, as you’re also doing activation and deactivation of the gameobjects in between triggers, so, This is what I suggest you :

void OnMouseDown( )

This function actually catches specific click on a specified Game Object, its not work with UI objects, It’ll need a collider attached and will identify which is clicked and what to do.

So, Basically for this to work you need gameobjects attached with a collider and script with this function on it, it will identify your button click dynamically, and always checks which gameobject is clicked, unlike OnClick ( ) which only identifies a click on the gameobjects.


Looks like I was explaining the problem badly, but I’ve actually worked out the issue now. What was happening was that because the menu element containing the button was getting deactivated when it was clicked, the EventHandler basically thought the button was still selected, and it seems it couldn’t change that since the button itself was now deactivated. Clicking through the buttons worked fine because you’re obviously selecting a new button and all that, but pressing escape through everything off so that it thought a disabled button was still selected. So, what was happening was that after pressing escape to close a menu and open a new one, the system still thought the old button was selected, so the next click caused that button to click rather than the one you think you are clicking.

Basically, I solved the problem by adding this to the OpenMenu function
GameObject myEventSystem = GameObject.Find(“EventSystem”);
myEventSystem .GetComponent().SetSelectedGameObject(null);