OnTriggerStay2D with Input.GetButtonDown not behaving properly?

Hey all,

I have this following script in a game object:

    void OnTriggerStay2D(Collider2D col){
        if(Input.GetButtonDown("Action")){
            Debug.Log("Got Key Press");
        }
    }

But the issue is, it only registers the first click of the action button, and not every time you click while you are in that trigger. When I take the Debug.log line out and place it outside of this if statement, it is logged continuously until the object exits the trigger. How do I solve this?

Thanks.

Input.GetButtonDown() will only return true for the 1 frame that the button first gets pressed. Physics functions, like OnTriggerStay2D(), do not occur every frame, but instead happen every time a certain amount of time passes (0.02 seconds by default). So it is possible that the physics functions do not get called for the 1 frame that Input.GetButtonDown() return true.

I suggest setting a bool for “isColliding” that you set true/false when you enter/exit the trigger, then you can check Input.GetButtonDown() AND the “isColliding” bool in an update() function.

That solved my problem thanks