• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by NathanSix · Jan 23, 2014 at 08:01 PM · javascripterrormathsyntaxexpression

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eric5h5 · Jan 23, 2014 at 11:19 PM 0
Share

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.

avatar image NathanSix · Jan 23, 2014 at 11:30 PM 0
Share

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!

avatar image Benproductions1 · Jan 23, 2014 at 11:51 PM 0
Share

@Eric5h5 Thanks, I guess... XD

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Simple syntax error that I can't seem to find 2 Answers

Syntax errors? 2 Answers

Script error with turrets 5 Answers

Error in switching weapons (activate/deactivate objects) 1 Answer

Make JavaScript throw a custom error? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges