Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by VoidLegion · Mar 14, 2016 at 04:48 AM · unity 5error messageelse

Invalid Expression Term 'Else'

For some reason or another the second 'else' in this void is throwing out an exception. None of the normal things that would cause this seem to be present (no semicolons on the end of an if statement for example) so I am unsure what the problem could be. I have tried restructuring the void a bit, but nothing seems to alleviate the issue. Any help would be deeply appreciated.

 void stateInAir(){
 
         HandleHorizontalInput();
 
         if (canJump) {
 
             CanDoubleJump = true;
             
             if (inputHoldTime < maxJumpButtonHoldTime && Input.GetKey (KeyCode.Space)) {
 
                 inputHoldTime += Time.deltaTime;
 
                 GetComponent<Rigidbody2D> ().velocity += new Vector2 (0, thePlayer.JumpAccel ());
 
             } else {
 
                 if (Input.GetKeyDown (KeyCode.Space) && CanDoubleJump == true) {
 
                     setState (PlayerStates.ENTER_JUMP);
 
                     CanDoubleJump = false;
     
                     canJump = false;
                 }
             } 
 
 
             else {
 
                 checkForGround ();
 
             if (onGround) {
                 
                     HandleLandOnGround ();
 
            }
         }
     }
 }
 
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

1 Reply

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

Answer by ZefanS · Mar 14, 2016 at 05:23 AM

Based on your current bracketing the statement goes like this:

 if (...)
 {
     //...
 }
 else
 {
    //...
 }
 else
 {
    //...
 }

An if-else statement can't have more than one else statement so you either need to use an else if (...) or restructure your brackets correctly.

As a side note, what you are referring to as a void is in fact called a method. The word void in this case indicates that the method has no return value. Method names should be formatted LikeThis.

Hope this helps!

Comment
Add comment · Show 4 · 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 VoidLegion · Mar 14, 2016 at 05:52 AM 0
Share

@ZefanS Understood. In that case, to be clear, what would be a more efficient way to structure my brackets? For the time being I will attempt to make due with an else if, but if there is a better long-term way to solve this issue i would love to know about it.

avatar image ZefanS VoidLegion · Mar 14, 2016 at 06:08 AM 0
Share

It's not so much a question of more efficient. It's illogical to have two else statements. That's why you can't. If I have a statement like:

 if (a == 1)
 {
     Debug.Log("a is 1");
 }
 else
 {
     Debug.Log("a is not 1");
 }

then the else catches all conditions in which a is not 1. In other words, having more than one else is redundant and will cause an error.

In this particular case, I'm not sure what to recommend because I don't know how your script is supposed to work. In any case I would recommend doing some reading on conditional statements to make sure you properly understand how to use them.

As a side note, you should post responses to answers as comments, not as new answers. See the User Guide

avatar image VoidLegion VoidLegion · Mar 14, 2016 at 06:20 AM 0
Share

Ah, understood, I think I improved it. I see what you mean about the else statements, and have now restructured the method not to use two. Thank you for taking the time to help me.

avatar image ZefanS VoidLegion · Mar 14, 2016 at 07:30 AM 0
Share

You're very welcome!

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

74 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 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 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 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

why my Unity Couldn't decompress package? 0 Answers

Survival Shooter Error CS0120 1 Answer

Assets/BallControl.js(17,55): BCE0034: Expressions in statements must only be executed for their side-effects. 0 Answers

Hi im getting this error in my script Assets/MouseManager.cs(11,27): error CS1525: Unexpected symbol `Vector3' 2 Answers

IndexOutOfRangeException: tanks index is invalid , i was trying to add new Tank in Tanks!!! game but get this error , iam new in coding 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