Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 gamezdragon · Mar 16, 2013 at 06:24 PM · if statementelse

'else' won't work in an if statement

Here is my code:

 #pragma strict
 
 var walkSpeed = 0.2;
 var runSpeed = 1;
 var flySpeed = 3;
 
 function Start () {
 
 }
 
 function Update () {
 if    (Input.GetAxis("Vertical"));
         animation.CrossFade("Walk_") = walkSpeed;
     else
         animation.CrossFade("Rest");
 
 }

Whenever I try to use 'else' this comes up:

BCE0044: expecting }, found 'else'.

Is there a new code for if statements instead of else? I've heard you use OR or || for something in if statements but none seem to work for me...

Does anyone have an answer?

Thanks

Comment
Add comment · Show 5
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 AlucardJay · Mar 16, 2013 at 06:23 PM 0
Share

You have a semicolon where it should not be at line 12. And I have no idea what you're trying to do at line 13.

First learn how to structure your conditionals before taking shortcuts by not using curly braces for single-line conditionals.

 if ( someCondition == true )
 {
     // use curly braces until you know what's going on
 }
 else
 {
     // code will be confusing if you don't understand the rules
 }

in the above example there is no semicolon after the if. SO your update should look something like :

 function Update () {
     if ( Input.GetAxis("Vertical") ) // no semicolon here
     {
         animation.CrossFade("Walk_"); // what is this? = walkSpeed;
     {
     else
     {
         animation.CrossFade("Rest");
     {
 
 }
avatar image gamezdragon · Mar 16, 2013 at 06:28 PM 0
Share

Thanks! The walkSpeed was the variable for how fast the dragon would move...

avatar image gamezdragon · Mar 16, 2013 at 06:30 PM 0
Share

and it still says: Unexpected token: else.

Am I supposed to use something other than 'else' in this version of Unity?

avatar image DryTear · Mar 16, 2013 at 06:52 PM 1
Share

fixing alucard -

 function Update(){
     if(Input.GetAxis("Vertical"))
     {
         animation.CrossFade("Walk_");
     }
     else
     {
         animation.CrossFade("Rest");
     }
 }
avatar image AlucardJay · Mar 16, 2013 at 07:13 PM 0
Share

ah, bugger, I did copy-paste those curly braces in the second code example. Thanks for pointing that out DryTear. You can see in the example I did display this properly (unedited I swear). So I shall leave my error and explain it here : yes I did stuff up with the OPs script, but the example at the top does clearly show opening and closing brackets, which just proves my point. $$anonymous$$ake sure you know what is going on ! Every open bracket must have a partner closing bracket. I have made some videos on basic scripting. I think it would be worthwhile for the OP to check them out : http://forum.unity3d.com/threads/173876-Program$$anonymous$$g-and-$$anonymous$$aking-Games-with-Unity-3D . Or the direct link to the conditionals video : http://youtu.be/EhS$$anonymous$$h$$anonymous$$-GIYU

Edit : Upvote to Benproductions1 and DryTear for their answers =]

Edit 2 : I was showing curly braces to indicate how the conditional should be set up. The first problem was the semicolon after the if argument before the outcome, the second problem was indeed the incorrect use of the CrossFade command.

1 Reply

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

Answer by Benproductions1 · Mar 18, 2013 at 03:12 AM

Ok, so since this has been answered in the comments and I don't have the privileges to convert comments to answers, I will post my own.

The problem you had, was that you were using the wrong syntax for and if statement. Like in many other languages, javascript and therefore unityscript uses {} curlybrackets for code wrapping, including an if-else statement. Also present in many other languages is the usage of a ; semicolen for EOL (end of line), this is used for every line of code that is executed individually.

Therefore your code should look like this:

 function Update() {
     if (/*Your expression for the if statement*/) {
         //code here
     }
     else {
         //code here
     }
 }

You can also stack an if on top of an else:

 if (/*your expression here*/) {
     //Your code
 }
 else if (/*Your second expression here) {
     //Your code
 }
 else {
     //Your code
 }

To use an or expression, in javascript and unityscript, you use || So if I want my code to happen when either a or b are true:

 true || true //evaluates as true
 true || false //evaluates as true
 false || false //evaluates as false
 false || (true || false) //evaluates as true

And is done similarly with &&

 true && true //evaluates as true
 true && false //evaluates as false
 false && false //evaluates as false
 true && (true || false) //evaluates as true

You can also allways use not, which inverts the evaluation and is done using a !

 !true //evaluates as false
 !false //evaluates as true
 !(true && true) //evaluates as false
 !(true && false) //evaluates as true
 !(false && false) //evaluates as true

I suggest learning these basics before starting anything big :)

Hope this helps, Benproductions1

Comment
Add comment · Show 1 · 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 gamezdragon · Mar 18, 2013 at 08:21 PM 0
Share

Thanks guys this was really helpful! Of course, now I have another question about if statements, but I guess that's what this forum is for

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.

Follow this Question

Answers Answers and Comments

14 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

Related Questions

If statement is being ignored and else is run instead 1 Answer

Why is one line of code correct but the other gives Errors? (Solved) 2 Answers

what code should i be using if im only wanting a function to happen if a raycast does not return a hit 1 Answer

I have a specific problem with my trigger 2 Answers

Keycode else 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges