OnTriggerStay only works about 6 times

Hello, I am currently learning Unity by following a very basic guide, and I noticed an issue with my game so far, and I was hoping to get some advice possibly? I hope this is in the right section of the forums, sorry if it’s not.

On certain objects and obviously for character dialogue later, I have a box collider that is a trigger for dialogue, using OnTriggerStay2D. When the player presses the Z key the dialogue box appears, but it usually takes 2 or 3 presses of Z for it to appear, intermittently (sometimes Z works immediately, sometimes it doesn’t), is my first problem.

My other problem is that this only works about six times before I have to move away and back into the collider in order to trigger the dialogue again. Is there a finite time with OnTriggerStay2D? Or is there something I’m missing?

A point in the right direction would be greatly appreciated. I’m not looking for people to write my code for me, only advice as I am still learning.

Thank you.

void OnTriggerStay2D(Collider2D other)
    {

        // If the player is colliding with the object
        if (other.tag == "Player")
        {
            // Check if the player presses the key Z
            if (Input.GetKeyUp(KeyCode.Z))
            {

                //Check to make sure the dialogue box isn't already running
                if (!dMan.dialogueActive)
                {

                    thePlayer.allowedToMove = false; 
                    dMan.dialogueLines = dialogueLines;
                    dMan.currentLine = 0;
                    dMan.ShowDialogue();

                } // END CHECK ACTIVE

            } // END CHECK PRESS Z

        } // END CHECK PLAYER

    } // END ONTRIGGERSTAY2D

More likely one of the inner if statements is getting tripped. You can put a Debug.Log at the top of the function to confirm that it continues to get called. My money would be on the dMan.dialogueActive one becoming false at some point.

Thank you for your reply!

Hmm. I am able to see that at the end of every of the dialogue, that my dialogueActive is properly being set to false, so when the player presses Z, and it’s false, it is then set to active to show the dialogue.

Actually, I just discovered while trying to test this, after I’ve gone through the 4 - 6 cycle of the dialogue working, if I actually keep the Player character in the trigger zone, but I move it against the object, it will show the dialogue with Z 1 time. Then it doesn’t work until I move the player again, and then it will show the dialogue one more time again. As soon as I move out of the trigger zone and back in, it works 4 - 6 times, then stops again, and it takes a ton of presses of Z to get the dialogue to appear again.

I hope that jumble makes sense…