Changing a value - code works, but doesn't? (C#)

if (Input.GetButton(“Sprint”)) speed = 200f;
if (Input.GetButton(“Aim”)) speed = 50f;
else speed = 100;
Sprint doesn’t change the value of speed, but Aim does.
However, when I replace Sprint’s command with a Debug.Log, it works as expected.

I’ve also tried this:

if (Input.GetButton("Sprint"))
			speed = 200;

“1” and “2” are displayed in the console, but the value of speed isn’t changed - and again, Aim does change it.

So, Sprint works - or should work, as evidenced by the Debug.Log - but can’t alter speed, even though the practically identical Aim-statement can.
What’s the cause of this Schrödinger’s code? How do I fix this?

Thank you in advance.

Hi AngellZlayer,

The problem is that the values always change after the 1st if statement as regardless of the key pressed, it will always check to see if aim is pressed, and if not, set the speed to 100.

Just add a else before the 2nd if and it should work.

if (Input.GetButton("Sprint")){
    speed = 200f;
} else if (Input.GetButton("Aim"){
    speed = 50f;
} else {
    speed = 100f;

Hope this explains the problem!