- Home /
Javascript syntax errors with basic expressions
The error I get is
Scripts/Gamble.js(17,23): BCE0044: expecting ), found '='.
if I double up that equals sign I get
Scripts/Gamble.js(13,18): BCE0034: Expressions in statements must only be executed for their side-effects.
var balance : float = 0.0;
var payOut : float = 0.0;
var betAmount : float = 100.0;
var profitOnWin : float = 0.0;
var rollUnderToWin : float = 0.0;
var diceNumber : float = 50.0;
var totalWin : float = 0.0;
public var winChance : float = 0.0;
function FixedUpdate (){
totalWin == payOut*betAmount;
profitOnWin == totalWin-betAmount;
rollUnderToWin == winChance;
if (winChance == 50.0){
payOut == 1.98;
}
if (winChance == 25.0){
payOut == 3.96;
}
if (winChance == 10.0){
payOut == 9.9;
}
//
//if button press, call function DiceRoll
}
function DiceRoll (){
diceNumber == Random.Range(0.0,100.0);
if (diceNumber <= winChance){
GUI.Label(screenRect, "You Win!");
//add profit to balance
balance+=profitOnWin;
}
if (diceNumber >= winChance){
GUI.Label(screenRect, "You Lose!");
//subtract betAmount from balance
balance-=betAmount;
}
}
I have no idea what I'm doing wrong with the syntax here, I've been making adjustments for hours and can't get it.
Answer by Benproductions1 · Jan 23, 2014 at 11:02 PM
Hello,
There is one key thing wrong in your script, but before I get to that, there are a couple other things I should mention:
The indentation level of open {
and closed }
should be the same:
if (comparison) {
}
//NOT
if (comparison) {
}
Absolutely everyone is in agreement, that you should leave a space between )
and {
:
if (comparison) {
//OR
if ( comparison ) {
//BUT NEVER
if(comparison){
Now onto the actual problem:
In UnityScript, just like in C#, there are different types of operators, such as x.y
or <
etc. These are all about using special syntax to achieve certain things.
There are primary:
//Member access with the "." operator
x.y
//Invoke methods
f(x)
//Type casting
x as y
//increasing using the increment operator
x++
//Getting the size of the object
sizeof(x)
There are the Unary:
//Positive
+x
//Negative
-x
//Not
!x
//Increment
++x
//Decrement
--x
There are the multiplicative:
//Multiply
x*y
//Divide
x/y
//Mod
x%y
Additive:
//Addition
x + y
//Subtraction
x - y
Shift:
//Right Shift
x >> y
//Left Shift
x << y
Relation:
//Larger Than
x > y
//Smaller Than
x < y
//With equals
x >= y
x <= y
//Type checking
x is y
Equality:
//Are Equal
x == y
//Are Not Equal
x != y
Logical:
//And
x & y
//Or
x | y
//XOr
x ^ y
Conditional:
//And
x && y
//Or
x || y
Assignment:
//Assign to
x = y
//Add do
x += y
//Subtract from
x -= y
//Multiply py
x *= y
//Divide by
x /= y
//lots more
All of this information can be found here and although it's a C# reference, UnityScript has most of the basic operators too.
Now lets look at the line where your error occurs and lets see which operator you are using:
rollUnderToWin == winChance;
if (winChance == 50.0){
So the first line you are doing a comparison between rollUnderToWin
and winChance
and then later you do a comparison between winChance
and 50.0
to check if they are equal. Why are you doing a comparison in the first one? Does that make any sense?
Lets look at what happens when we use a single =
:
rollUnderToWin = winChance;
if (winChance = 50.0){
First we assign winChance
to rollUnderToWin
and then we check, we assign (WTF) winChance
the value of 50.0
... That doesn't make any sense does it?
I think from this you should be able to figure out what you need to do to make it work.
Hope this helps,
Benproductions1
I hereby present to you the UnityAnswers 2014 Award for $$anonymous$$ost Severe Information Overkill in an Answer. ;) It's all good info, though I posted a TL;DR answer anyway.
Thank you so much for this Ben. You solved both of my problems and gave me a lot of information that I needed! You really helped me here!
Also about the winChance = 50 thing, it is supposed to be
if winChance is 50, then payout is 1.98... I had it set up weird, That error with the space between ) and { and the misplacement of the } was really throwing me off.
Thank you again, Sir!
Answer by Eric5h5 · Jan 23, 2014 at 11:17 PM
You would never use == when you are assigning values to a variable; that's only for comparing two values. == for comparison, = for assignment.
Your answer
Follow this Question
Related Questions
Script error with turrets 5 Answers
Simple syntax error that I can't seem to find 2 Answers
Syntax errors? 2 Answers
I got this error when i create a new JS script and open it 0 Answers
fake error 1 Answer