Search Unity

  1. Tune in to get all the latest Unity news live from Berlin on June 19, 6pm CEST. Set up a YouTube reminder!
    Dismiss Notice
  2. Unity 2018.1 has arrived! Read about it here
    Dismiss Notice
  3. Scriptable Render Pipeline improvements, Texture Mipmap Streaming, and more! Check out what we have in store for you in the 2018.2 Beta.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  5. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  6. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

error CS1525: Unexpected symbol `else'

Discussion in 'Scripting' started by GhulamJewel, Jun 14, 2018 at 10:46 PM.

  1. GhulamJewel

    GhulamJewel

    Joined:
    May 23, 2014
    Posts:
    319
    Trying to create a nested else statements but getting this error? Any advise what to remove or include? Thank you.

    MouseOrbit.cs(55,5): error CS1525: Unexpected symbol `else'



    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.    //[AddComponentMenu("Camera-Control/Mouse drag Orbit with zoom")]
    5.     public class MouseOrbit : MonoBehaviour
    6. {
    7.     public Transform target;
    8.     public float distance = 5.0f;
    9.     public float xSpeed = 120.0f;
    10.     public float ySpeed = 120.0f;
    11.    
    12.     public float yMinLimit = -20f;
    13.     public float yMaxLimit = 80f;
    14.      
    15.     public float distanceMin = .5f;
    16.     public float distanceMax = 15f;
    17.    
    18.     public float smoothTime = 2f;
    19.  
    20.     float rotationYAxis = 0.0f;
    21.     float rotationXAxis = 0.0f;
    22.    
    23.     float velocityX = 0.0f;
    24.     float velocityY = 0.0f;
    25.  
    26.     public float Seconds = 0.5f;
    27.    
    28.     // Use this for initialization
    29.     void Start()
    30.     {
    31.  
    32.         Vector3 angles = transform.eulerAngles;
    33.         rotationYAxis = angles.y;
    34.         rotationXAxis = angles.x;
    35.        
    36.         // Make the rigid body not change rotation
    37.                   if (GetComponent<Rigidbody>())
    38.         {
    39.             GetComponent<Rigidbody>().freezeRotation = true;
    40.         }
    41.     }
    42.    
    43.         void LateUpdate ()
    44.     {
    45.         if (target) {
    46.             if (ControlFreak2.CF2Input.GetMouseButton (0)) {
    47.                 velocityX += xSpeed * ControlFreak2.CF2Input.GetAxis ("Mouse X") * 0.02f;
    48.                 velocityY += ySpeed * ControlFreak2.CF2Input.GetAxis ("Mouse Y") * 0.02f;
    49.  
    50.             } else {
    51.  
    52.                 velocityX += xSpeed * Input.GetAxis ("Horizontal2") * 0.02f;
    53.                 velocityY += ySpeed * Input.GetAxis ("Vertical2") * 0.02f;
    54.  
    55.             } else {
    56.  
    57.             velocityX += xSpeed * Input.GetAxis ("Horizontal3") * 0.02f;
    58.             velocityY += ySpeed * Input.GetAxis ("Vertical3") * 0.02f;
    59.  
    60.         }
    61.    
    62.             rotationYAxis += velocityX;
    63.             rotationXAxis -= velocityY;
    64.            
    65.             rotationXAxis = ClampAngle(rotationXAxis, yMinLimit, yMaxLimit);
    66.              
    67.             Quaternion fromRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0);
    68.             Quaternion toRotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0);
    69.             Quaternion rotation = toRotation;
    70.            
    71.             distance = Mathf.Clamp(distance - ControlFreak2.CF2Input.GetAxis("Mouse ScrollWheel")*Seconds, distanceMin, distanceMax);    
    72.                
    73.                
    74.             Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
    75.             Vector3 position = rotation * negDistance + target.position;
    76.                          
    77.             transform.rotation = rotation;
    78.             transform.position = position;
    79.            
    80.             velocityX = Mathf.Lerp(velocityX, 0, Time.unscaledDeltaTime * smoothTime);
    81.             velocityY = Mathf.Lerp(velocityY, 0, Time.unscaledDeltaTime * smoothTime);
    82.         }
    83.        
    84.     }
    85.    
    86.           public static float ClampAngle(float angle, float min, float max)
    87.     {
    88.         if (angle < -360F)
    89.             angle += 360F;
    90.         if (angle > 360F)
    91.             angle -= 360F;
    92.         return Mathf.Clamp(angle, min, max);
    93.     }
    94. }
    95.  
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    3,672
    You can't have two else statements with a single if statement.

    Correct way
    If
    else if
    else

    incorrect
    if
    else
    else
     
    GhulamJewel likes this.
  3. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    567
    They don't nest that way. There can be zero or many "else if" but only zero or one "else" statements per "if" :
    Code (CSharp):
    1. if( condition_1 )
    2. {
    3. }
    4. else if ( condition_2 )
    5. {
    6. }
    7. else if ( condition_n )
    8. {
    9. }
    10. else
    11. {  // If none of the above then do this.
    12. }
     
    GhulamJewel likes this.
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    3,672
    Yeah, what @Doug_B said. In a nicer formatted less lazy way than me. :)
     
  5. GhulamJewel

    GhulamJewel

    Joined:
    May 23, 2014
    Posts:
    319
    Thanks I did try something like that but is there a way to have no condition with "else if" basically it is just a camera script that works with mouse and keyboard as well as Xbox and PS4 controllers. The annoying thing the controllers have two different axis names hence why doing two different else. First one does have a condition because have to click mouse then move camera but the controllers should just work by moving the right analog stick. It works with only one else statement but not sure how to do it with two. Thanks.
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    3,672
    There is no way to have two else. You should have another condition you should be checking for, some check for the two sticks.

    But as you have it written, do you need two else statements? Would putting all that code work? Cause unless you have a condition to check against, you're currently just telling it to check for one condition, if that isn't true, then run this other code down here. Since that is all an else does.
     
    Doug_B likes this.
  7. GhulamJewel

    GhulamJewel

    Joined:
    May 23, 2014
    Posts:
    319
    Thanks there is a plugin called InControl which I might use as solves all your input problems. I am hesitate because I am already using Controlfreak plugin but will experiment. I Did try something like below to get one or another axis with the || but I get (53,77): error CS1525: Unexpected symbol `+='. Thanks for your advise!

    Code (CSharp):
    1.         if (target)
    2.         {
    3.             if (ControlFreak2.CF2Input.GetMouseButton (0)) {
    4.                 velocityX += xSpeed * ControlFreak2.CF2Input.GetAxis ("Mouse X") * 0.02f;
    5.                 velocityY += ySpeed * ControlFreak2.CF2Input.GetAxis ("Mouse Y") * 0.02f;
    6.  
    7.             }else{
    8.  
    9.                 velocityX += xSpeed * Input.GetAxis ("Horizontal2") * 0.02f || velocityX += xSpeed * Input.GetAxis ("Horizontal3") * 0.02f;
    10.                 velocityY += ySpeed * Input.GetAxis ("Vertical2") * 0.02f || velocityX += xSpeed * Input.GetAxis ("Vertical3") * 0.02f;
    11.  
    12.             }
    .
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,642
    Hmm.. maybe something like (only showing the horizontal portion):
    Code (csharp):
    1. else {
    2.    float horz = Input.GetAxis("Horizontal2");
    3.  
    4.    if(horz == 0) horz = Input.GetAxis("Horizontal3");
    5.  
    6.    velocityX += xSpeed * horz * 0.02f;
    7. }
    That assumes you don't know which controller might be in use, but should work.
     
  9. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    3,672
    Yeah, @methos5k posted a similar thought to what I had.

    But, I think Input.GetAxis always returns 0 if it is just at base position also, doesn't it?

    I'm assuming -1 is one direction at max and 1 is the other. So if you get 0, I suppose you just don't do anything with it.

    I haven't tried to hook into a controller yet, so I'm just thinking of the logic of it.
     
  10. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,642
    Exactly.. I've never actually used a controller. My code would be a naive sort of check, seeing if 1 controller gives you any data, and only if not, check the other one. You'd still have zero if they neither were 'moved', I imagine.

    My other idea was to get them both and clamp them, but that seems a bit silly. :)
     
  11. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    1,602
    You are missing the closing bracket for if (target)
     
  12. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,642
    That might have just been omitted from the post.
    This line of code doesn't look right.
    Code (csharp):
    1. velocityX += xSpeed * Input.GetAxis ("Horizontal2") * 0.02f || velocityX += xSpeed * Input.GetAxis ("Horizontal3") * 0.02f;
    :)
     
  13. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    1,602
    @methos5k Agreed, best to always post all the code. As you point out, in pseudocode, velocityX is equal to something or (and at the same time, ignoring the or) velocityX is equal to something else. Yeah, not quite right.

    Might be best to first write out the logic in pseudocode (sentences), then code it.