JavaScript global variable update not registering in engine.

I created a simple JavaScript to move an object around in 2D space.


    #pragma strict
    
    var speed : float = 10.0;
    
    function Update()
    {
    	Debug.Log(speed);
    	
    	// Map player movement to the directional keys.
    	var moveX : float = speed * Input.GetAxis("Horizontal") * Time.deltaTime;
    	var moveY : float = speed * Input.GetAxis("Vertical") * Time.deltaTime;
    	
    	transform.Translate(moveX, 0, 0);
    	transform.Translate(0, moveY, 0);
    }

I noticed that changing the speed variable did not change the object’s movement speed whatsoever, so I added the Debug.Log() statement. I found that the during the first run, the console would print out the value of 10, but when I change the speed variable to something else, say 20, the value that the console outputs stays at 10. However, when I move speed into the Update function, the console would output the correct value of speed.

Is this a bug?

Have you assigned that script to an object already? ‘speed’ is an exposed variable and, if you have assigned the script to an object, it will be editable in the object itself.

So, even if you change the value of ‘speed’ in the JS file, the effective value for a specific object won’t change anyway, because the “reference” of that variable is now in the object’s Inspector.

Click on the object, look at the Inspector, and set the value to ‘20’ from there.