Event.PopEvent vs Input.GetButtonDown

I’m putting together input systems for a primarily gamepad-driven game. When I press a button to get a menu up, the input is still registered by the next menu panel and immediately cancels the panel. Think of the way you use an “interact” button to start dialog with an NPC and press “interact” again to finish the conversation.

My first thought was that I would treat it like UI when I’m making commercial desktop apps, in which you would set a UI event to handled. But that seems to only work with Event.PopEvent. And that doesn’t seem to work with the input axes I set up in Edit → Project Settings → Input.

What’s the best way to check both keyboard and gamepad input for an event and to keep that event from firing again on the newly-enabled menu?

I’m running Unity 2017.1.0p4 on Windows 10.

The Event class is part of the old OnGUI system and should really only be used for editor scripting.

Which means it’s worth fixing the bug you have with Input. Care to share code?

Sure thing. It’s a bit much, since I’m trying to write more reusable systems, so bear with me.

Code Snippets

// called by pause menu button OnClick handler, triggered by "Submit" input on a button
        public void Save()
        {
            Debug.Log("Save Complete");
            TextManager.Instance.DisplayModal("Save Complete");
        }
//TextManager.cs
    public void DisplayModal(string text, int xPercent = 0, int yPercent = 0)
    {
        modalTextBoxRef = Instantiate(panelModalPrefab, this.transform, false);
        modalTextBoxRef.GetComponentInChildren<Text>().text = text;
        this.StartListening();
    }
// TextManager.cs for modal pop-up
    protected override void ReceiveInput()
    {
        base.ReceiveInput();

        if (Input.GetButtonDown("Submit") ||
            Input.GetButtonDown("Cancel"))
        {
            this.StopListening();
            Destroy(modalTextBoxRef);
            modalTextBoxRef = null;
        }
    }
  • the return key triggers the OnClick event of a button
  • The button calls my GM.Save() method
  • GM.Save() calls my TextManager.DisplayModal() method
  • the TextManager.DisplayModal() method instantiates a “Save Complete” prefab that begins listening for input so that the player can dismiss it when done reading.
  • The Modal dialog panel is immediately dismissed, even though I only pressed return once. I double-checked in the VS debugger, and it is definitely being triggered by Input.GetButtonDown(“Submit”).

The most confusing part of all this is that it only happens once per run. Every subsequent Save works as normal.