- Home /
Error expecting ) found |
So i have an error in my script that i cant fix and i cant test my FPS game without. I know this is an easy fix but i am new to javascript and i dont even have a key that allows me to type | but i am able to copy and paste it.
The series of videos i am watching told me to put || but then i get the error in the title. So i tried putting only one | and then it came up with a different error
Operator '|' cannot be used with a left hand side of type 'float' and a right hand side of type 'float'.
I dont even know what that means!! If you know what is wrong then please help or if you know how to fix it i could really use your help too.
var Fire : String;
var Reload : String;
var Idle : String;
var Wlak : String;
function Update()
{
if(Input.GetAxis("Vertical") | Input.GetAxis("Horizontal")){
animation.Play(Walk);
}
else
{
animation.Play(Idle);
}
}
function FireAnim()
{
gameObject.animation.Play(Fire);
}
function ReloadAnim()
{
animation["Reload"].speed = (animation["Reload"].clip.length / Gun.ReloadTTime);
animation.Play(Reload);
}
on some keyboards the "|" key is displayed with a gap inbetween (kinda like a vertically stretched ":"). I've never heard of a keyboard without the "|" key.
Answer by perchik · Aug 22, 2013 at 08:13 PM
So a single bar means Bitwise OR. Every number is stored as a bunch of 1s and 0s, and bitwise OR compares those 1s and 0s.
What you want is actually the double bar ||
I wonder if perhaps you are running into your error because the character for the bar that you are copying is wrong?
Answer by Loren-Logic · Aug 22, 2013 at 08:53 PM
Try the shift-\ key over the ENTER key. Try isolating both OR clauses with more parentheses, and add the arguments, like this:
if((Input.GetAxis("Vertical") == true) || (Input.GetAxis("Horizontal") == true)){ ... } I've run across occasions when .net can't OR correctly without explicit precedence. Although you certainly did have an error in your code with just one pipe (|) symbol. You tried to do a bitwise comparison of two floating decimal values; your values to the left and to the right of the equal sign were both 'floats,' when integers are required. The double-pipe compares TRUE to TRUE or FALSE to FALSE or TRUE to FALSE or FALSE to TRUE. That's what you want.
Additional parentheses are not required, and comparing to true will completely prevent it from working.
In JS this works:
if(Input.GetAxis("Vertical") || Input.GetAxis("Horizontal"))
but if you compare value from GetAxis to boolean:
if(Input.GetAxis("Vertical") == true || Input.GetAxis("Horizontal") == true)
then it won't work.
Answer by lancer · Aug 22, 2013 at 08:57 PM
Input.GetAxis returns a float, you need to do something like this:
if(Input.GetAxis("Vertical") > 0| Input.GetAxis("Horizontal") > 0){
animation.Play(Walk);
}
else
{
animation.Play(Idle);
}
This is wrong answer. Firstly, because bitwise operator is still there. Secondly, because in JS it is enough to do:
if(Input.GetAxis("Vertical") || Input.GetAxis("Horizontal"))
You don't have to compare results to any number.
And thirdly, because you only test using greater then operator. And what if negative float is returned?
Input.GetAxis()
returns 0 if that axis is at it's default position. When you do an if on a float, if it's 0 it returns false, else it returns true. Usually this code is used to handle an axis that has moved, so the if statement only enters the body if the axis isn't at it's default position.