How to use nested if else statements in void Update()?

According to the lecture, when player pressed Enter key, the system won the game and inside that same if else statement, I would like to ask the player to continue playing or terminate?

Observed:
The system loops at void Update() constantly, not receiving inputs for the nested if else statement. Nothing happens when I pressed Y or N keys.

Expected:
After pressing Enter key, go inside if else, loop for user input of Y = Yes or N = No to continue playing or terminating the game.

These two images are the coding and observed output respectively.

The clue is in “update is called once per frame”

So your user presses Return, the update function will start again before they even get chance to press Y. You are treating it as if the code will pause execution for the input. It doesn’t unfortunately.

You’d have to hold down Return and Y at the same time for this to work

You will have to manage a state or flag to get this to work. Maybe try the Text 101 lesson and this will be a bit clearer

Will come back to this question after Text 101, thank you very much!

Basically your app will be in one or several states and the Update calls the relevant function for that state.

The specific state function will then handle only the inputs relevant to that state.

I think it would be a good exercise to apply what you learn in that later lesson to try and fix your original problem. They are somewhat different apps (not hugely though) but the knowledge there would be enough to adapt it.

I was trying the exact same thing, Henry.

And thanks for helping both of us out, jmp909. :wink:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms