How to disable mouse click detection in game when clicking exit menu button?

So i made a script that instantiates a cube on Input.GetButtonUp(“Fire1”).

I also have a menu and anytime i click the button to close the menu the cube instantiates (obviously not my intention). I have no idea how to get around it. I tried checking that the menu is closed before instantiating, but it doesn’t work. Also tried counting frames after the menu closes. Tried moving the close menu function after the instantiate function so the instantiate function would be called a frame after the close menu function. nothing.

Does anyone have any ideas? The script this is in handles a lot of other things, so the code is too long to post in here. But I could try if it would make a difference.

You could add an additional check (EventSystem.current.currentSelectedGameObject == null) to your Input.GetButtonUp(“Fire1”). This way, you will only instantiate a cube when your mouse isn’t hovering above a button.

Make sure to add using UnityEngine.EventSystems aswell.