Unity is telling me I have an error in my java script but I just can’t seem to find it. Help?
#pragma strict;
function Start () {
var forwardSpeed:float=3;
var turnSpeed:float=2;
}
function Update () {
var forwardMoveAmount:Input.GetAxis("Vertical")*forwardSpeed;
var turnAmount-Input.GetAxis("Horizontal")*turnSpeed;
transform.Rotate(0,turnAmount,0);
}
there are a few problems here;
firstly, there’s a semicolon at the end of your “#pragma strict;” - that’s causing an error.
Secondly, lines 8 and 9 you’ve got
var forwardMoveAmount:Input.GetAxis("Vertical")*forwardSpeed;
var turnAmount-Input.GetAxis("Horizontal")*turnSpeed;
Following the forwardMoveAmount and turnAmount, you’ve got a colon and a hyphen; as far as I’m aware (I’m not too familiar with javascript) when declaring a variable you can use a colon to specify the type, followed by an “= value;” (as seen in your forwardSpeed and turnSpeed variables in your Start() function).
If you replace the colon/hyphen with an equals, it should be fine.
Finally, you’re trying to use the forwardSpeed and turnSpeed variables made in your start function - unfortunately, they’re being made within the scope of the Start() function, so once it leaves that scope the variables no longer exist and will give you another error.
You can declare them outside of the function and it should work fine though.
A working version of what you gave would be as follows;
#pragma strict
var forwardSpeed:float=3;
var turnSpeed:float=2;
function Start () {
}
function Update () {
var forwardMoveAmount = Input.GetAxis("Vertical")*forwardSpeed;
var turnAmount = Input.GetAxis("Horizontal")*turnSpeed;
transform.Rotate(0,turnAmount,0);
}
forwardSpeed, turnSpeed private variables defined in start
you can’t access them in update()
define them outside start
It’s tell you what type of error and where it occurs. Well but your problem is in line 8 and 9. I’m not sure what you are trying to do it and how, but your format is all wrong. Do this instead it will make you coding a lot easier.
var forwardSpeed: float;
var turnSpeed: float;
var forwardMoveAmount;
var turnAmount;
//Var can not be assigned a value outside of a function.
function Start () {
forwardSpeed=3;
turnSpeed=2;
}
function Update () {
forwardMoveAmount = Input.GetAxis("Vertical")*forwardSpeed;
turnAmount = Input.GetAxis("Horizontal")*turnSpeed;
transform.Rotate(0,turnAmount,0);
}
It is common practice to declare var outside of the functions at the top of the script.
Public var can be accessed by any script
Private can only be accessed by functions inside that script and cannot be accessed (Even for data) by other scripts.
If this is a movement script you need to work on it. You will be calling this function ever frame and can crash your client, so care. try using like an if statement.
if (Input.GetKeyDown(KeyCode.W)) {
//Do something to move forward
}
This way you don’t call it non stop and you can see what it actually does.
You have a number of errors in this script. As @Eric5h5 indicated, you can double click on the error in the console and it will highlight the error in Mono. Some of the problems:
- In the first line, “#pragma strict” should not have a ‘;’ at the end
- forwardSpeed and turnSpeed should not be declared inside Start() since you will use then in Update(). Move them above Start().
- Your syntax for declaring and assigning variables is wrong on lines 8 and 9