Confusion about local variable in a script

So I am looking at the scripting tutorial where it describes the script for creating a simple FPS camera, and I decide to rotate the camera. This is my script

var speed = 5.0;

function Update()
{
  // Keep track of rotation on the Y (up) axis
  var rotateY = 0;

  // As per the tutorial
  var x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
  var z = Input.GetAxis("Vertical") * Time.deltaTime * speed;

  // Use the Q key to rotate
  if (Input.GetKey("q"))
  {
    rotateY = 1 * Time.deltaTime * speed;

    print(rotateY); // always output zero!
  }

  transform.translate(x, 0, z);

  transform.rotate(Vector3.up * rotateY)
}

For some reasons, this code does not work. Whenever I hold down the Q key, a value of zero is output. However, if I remove the var rotateY = 0 from the top and change the if block to look like this

if (Input.GetKey('q'))
{
   var rotateY = 1 * Time.deltaTime * speed;
   ....
}

The code works. Why so? For the first script, when Update is invoked, it sets rotateY to zero. The Q key is held down, so rotateY is calculated, but yet it becomes zero. (I did try print (1 * Time.deltaTime * speed and the output is not zero).

Any suggestion?

With the first example, you’re defining rotateY as an integer, and since Time.deltaTime is always less than 1 (unless you’re getting 1fps or less…), it’s not possible for the result to be anything other than 0. Also, 1*anything is itself, so there’s no point in multiplying anything by 1. In the second example, you’re defining rotateY as a float.

–Eric

Ah I see, so the data type is fixed at the point of assignment. So all I have to do is to change the first line of the script to

function Update() 
{
  var rotateY = 0.0;
}

and it works.

Thanks!