(Input.Keycode) don't get the second input in a script

Hello everyone,

So I’ve got this code on something that allows me to select it.

When the gameobject is selected, I’ve included this chunk of code:

if(Input.GetKeyDown(KeyCode.U))
{

			if(Input.GetKeyDown(KeyCode.F))
			{
				Debug.Log("FirepowerUpgrade");
			}
			if(Input.GetKeyDown(KeyCode.S))
			{
				Debug.Log("SpeedUpgrade");
			}
			if(Input.GetKeyDown(KeyCode.R))
			{
				Debug.Log("RadiusUpgrade");
			}
		}

Strangely, the console doest detect the first input (the U key) but never reaches the next buttons and I don’t have a clue why.

Anyone can help?

GetKeyDown is only true for one frame (the frame the key is pressed), this means that it checks to see if any other key is pressed for that frame only. You want to use GetKey for the U key, since it returns true as long as it’s down. GetKeyDown for the other keys should be okay.

if(Input.GetKey(KeyCode.U)) {
	if(Input.GetKeyDown(KeyCode.F))
	{
		Debug.Log("FirepowerUpgrade");
	}
	if(Input.GetKeyDown(KeyCode.S))
	{
		Debug.Log("SpeedUpgrade");
	}
	if(Input.GetKeyDown(KeyCode.R))
	{
		Debug.Log("RadiusUpgrade");
	}
}

Resources: GetKeyDown GetKey

I like what you are suggesting, but, in a way, poses me a problem. It would mean that I HAVE to keep “U” down to access to my upgrade status.

But I think I may get another answer to my problem. I will tweak my code so that it turns a bool at “true” which will then allow me to access those other modes.

Thanks, you made me think! (And your answer is interesting!)

Here’s my solution:

if(Input.GetKey(KeyCode.U))
{
Debug.Log(“Upgrade mode”);

			canUpgrage = true;
			
		}
		
		if(canUpgrage)
		{
			if(Input.GetKeyDown(KeyCode.F))
			{
				Debug.Log("FirepowerUpgrade");
				canUpgrage = false;
			}
			if(Input.GetKeyDown(KeyCode.S))
			{
				Debug.Log("SpeedUpgrade");
				canUpgrage = false;
			}
			if(Input.GetKeyDown(KeyCode.R))
			{
				Debug.Log("RadiusUpgrade");
				canUpgrage = false;
			}
		}

And it works ^^