[Question] Input.GetKeyDown Bug?

I was updating the Prison text adventure game, and came across an issue with a nested Input.GetKeyDown command. What I mean is, I have a choice to look at the mirror. In the same block of code, the player is prompted to press L to look closer at the mirror. Problem is when L is pressed, it only continues to the next prompt for a split second and returns to the Look Closer prompt.

In C# outside of Unity you can get player input like:

if (userInput.Equals('l')) {
    //do something
}

If you want a second set of choices you can simply make a new input variable like this:

if (userInput.Equals('l'))
{
    //do something

    if (userInput2.Equals('l')) {
    //do something
    }
}

Is there a way to do this with Input.GetKeyDown?

I could use Input.GetKey, but then I would have to hold it down for the next part to stay on screen (although that could be a useful feature for picking up items…).

1 Like

Hello,
This problem happens because you don’t changed the state after the player press L, so the update function will run the Mirror() from the beginning on the next frame. For this to work you would have to re structure the whole game, IMO the best option would be to just create an state for when the player press L within the mirror state and treat it like the other states (and dependent on the Update method too).

2 Likes

That’s how I had it before, but it was getting confusing to keep track of using so many different methods. I was trying to see if I could condense all the related methods into one.

I guess I will just have to keep them all separate.

I understand, I don’t like this structure neither, because of the number of methods that you will eventually have to create, even more if you want to expand the game. Although I haven’t found a better solution to make it work :frowning: