Is there a reason GetKeyDown doesn't work with this code but GetKey does?

So I’m writing code that would control whether or not a player is swimming, climbing a ladder, ect. What the state change would do is in essence change the control scheme of the player, example: if ‘playerState’ is “normal” gives you your typical platformer controls, walk left, walk right, jump ect. However if the ‘playerState’ is “climbing” restricts you to moving only up and down, because you would be climbing on a ladder

Anyway here is the relevant code:

	public string playerState = "Normal";

	void Update () {
		bool contextHit = Input.GetKeyDown(KeyCode.F);
		bool contextHold = Input.GetKey(KeyCode.F);

	if(contextHit == true) {
			print ("F key Hit!");
			playerState = "climbing";
		} 

		if (contextHold == true) {
			print("F key Held!");
			playerState = "climbing";
		} 

}

So here’s the problem, I initially had the first if statement (contextHit) as my code and the state was NOT changing, however it was reading the fact that the F key is hit. The control scheme or ‘playerState’ did not change, but “F key Hit!” was being written to console.

However when I commented it out and used ‘contextHold’ not only did it register the key hit and write to console “F key Held!” but the ‘playerState’ changed to “climbing” and the controls changed appropriately.

So my question is twofold

  1. Any comments on WHY this is?

  2. Are there any alternatives should I have to make a command that requires the player to NOT hold down a key/button?

My first guess is that you are overwriting it somewhere later down the function or something along those lines. I would see if the playerState becomes "climbing" during the frame its pressed.

So either print() the playerState immediately after setting it (like the next line), or set a breakpoint there and then run it using a Debugger. If it is the correct state during that frame, then your problem is somewhere else.