Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Bug } Expected, yet is in the right spot

Discussion in 'Scripting' started by Vacczz, Aug 12, 2020.

  1. Vacczz

    Vacczz

    Joined:
    Aug 12, 2020
    Posts:
    6
    Hey, im new to unity and I've been putting together a simple little game based of of Dani's (https://www.youtube.com/channel/UCIabPXjvT5BVTxRDPCBBOOQ) fps game (Karlson). I tried putting in a crouching mechanic, but i've run into an error where there is a } expected, yet there is already one there, assigned to the specified spot, linked to a starting {. I've tried everything to fix it but it still shows the same error (Assets/Crouch.cs(26,111): error CS1513: } expected). i think this is just a bug for Visual Studio, but if you can help me out, please reply to this post. Here is the code.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5. public class Crouch : MonoBehaviour
    6. {
    7.     public CharacterController controller;
    8.     public float newHeight;
    9.     private float oldHeight;
    10.  
    11.  
    12.     void Start()
    13.     {
    14.         controller = GetComponent<CharacterController>();
    15.         oldHeight = controller.height;
    16.  
    17.     }
    18.  
    19.     void Update()
    20.     {
    21.  
    22.         if (Input.GetKey(KeyCode.LeftControl)) //
    23.             Input.GetKey / GetButton / GetAxis;
    24.  
    25.         controller.height = newHeight;
    26.         Vector3 newPos = new Vector3(transform.position.x, transform.position.y + 0.5f, transform.position.z);
    27.    
    28.             else if (Input.GetKeyUp(KeyCode.LeftControl)) //
    29.             Input.GetKeyUp / GetButtonUp / GetAxisUp;
    30.            
    31.             controller.height = oldHeight;
    32.            
    33.     }
    34.  
    35. }
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Your "else if" on line 28 doesn't make any sense, as it is not following an "if" statement. I'm also assuming your lines 23 and 29 are copy/paste errors with comments that are supposed to be on the end of the previous lines.

    Did you actually mean to write this? Though I don't understand what you're doing with newPos, since you're not using it.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5. public class Crouch : MonoBehaviour
    6. {
    7.     public CharacterController controller;
    8.     public float newHeight;
    9.     private float oldHeight;
    10.  
    11.  
    12.     void Start()
    13.     {
    14.         controller = GetComponent<CharacterController>();
    15.         oldHeight = controller.height;
    16.  
    17.     }
    18.  
    19.     void Update()
    20.     {
    21.  
    22.         if (Input.GetKey(KeyCode.LeftControl)) //Input.GetKey / GetButton / GetAxis;
    23.         {
    24.             controller.height = newHeight;
    25.             Vector3 newPos = new Vector3(transform.position.x, transform.position.y + 0.5f, transform.position.z);
    26.         }
    27.         else if (Input.GetKeyUp(KeyCode.LeftControl)) //Input.GetKeyUp / GetButtonUp / GetAxisUp;
    28.         {
    29.             controller.height = oldHeight;
    30.         }  
    31.     }
    32.  
    33. }
    I'd get in the habit of using curly braces to contain the code to run when your "if" statements resolve to true, as doing so makes your code less prone to accidentally writing this kind of error. When you get more comfortable later, then come back to skipping curly braces for brevity when an "if" statement should only run a single line of code. Just my recommendation.
     
    Last edited: Aug 12, 2020
  3. Vacczz

    Vacczz

    Joined:
    Aug 12, 2020
    Posts:
    6