- Home /
+= operator doesn't work (Javascript)
Newest code, speed just remains 0. When I add var to the second one, speed doesn't remain 0.
var speed = 0;
var accel = 10;
var x = 0;
var y = 0;
var z = 0;
function Update () {
speed = speed + Input.GetAxis("Vertical") * Time.deltaTime * accel;
z = z + speed;
print(speed);
transform.Translate(x, 0, z);
}
EDIT: Never mind, it works now. What the problem was, is that "accel" is just too small to actually make a difference, Unity prints this as 0.
Post more code so we can see what your doing exactly
Actually, I did keep the declarations on top. I'm really not a program$$anonymous$$g noob but it just doesn't work. I have programmed Actionscript 3, which is very similar, so I kind of know how declaring variables work.
The point is, that when I don't use var (only for the declaration), nothing happens.
Answer by MacMac098 · Jun 05, 2011 at 02:31 PM
var speed : float = 10;
function Update () {
var z = Input.GetAxis("Vertical") * Time.deltaTime * speed;
var x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
transform.Translate(x, 0, z);
}
Answer by Graham-Dunnett · Jun 02, 2011 at 05:00 PM
var speed = 0;
var accel = 10;
var x = 0;
var y = 0;
var z = 0;
function Update () {
speed = speed + Input.GetAxis("Vertical") * Time.deltaTime * accel;
z += speed;
transform.Translate(x, 0, z);
}
Note that you use var to tell the compiler about your variables. Once you have declared them don't use the var keyword when you use the variable.
Yep, that's what I did. It solves the problem but it's still silly and I'm used to using +=. removing the var completely stops it from working somehow.
$$anonymous$$eep the var in the declaration up top. Leave it out later when you use the variable.
The reason why removing var works is because by using var you are actually creating a new variable local to your method, when you obviously want to assign it to your class variable of the same identifier. This becomes a problem of scope.
Doesn't really make any sense to have "var z += speed;", because what should it add speed to? Zero? Infinity?
Answer by Wolfos · Jun 05, 2011 at 02:42 PM
Thanks Mac! What is the standard datatype it uses? Integer? Coming from Actionscript, I assumed Javascript was kind of the same in managing variables as Flash. It's not. I'll remember that it's not Actionscript and I'll follow some Javascript tutorials.
It inferes the type from the initialization value, if any (or from other expressions in the context). If you had written:
var speed = 0.0;
the compiler would assume speed as a float.
Personally, I don't believe in type inference, so I always declare explicity all variable types (tempting $$anonymous$$urphy's Laws is not a good practice...)
Answer by MacMac098 · Jun 05, 2011 at 02:45 PM
var Speed : float = 0;
var accel : float= 10;
private var RelativeSpeed : float = 0;
private var x = 0;
private var z = 0;
function Update () {
Speed += Input.GetAxis("Vertical") * Time.deltaTime * accel;
z = z + Speed;
Speed -= RelativeSpeed;
print(Speed);
transform.Translate(x, 0, z);
}