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.
using UnityEngine;
using System.Collections;
public class Crouch : MonoBehaviour
{
public CharacterController controller;
public float newHeight;
private float oldHeight;
void Start()
{
controller = GetComponent<CharacterController>();
oldHeight = controller.height;
}
void Update()
{
if (Input.GetKey(KeyCode.LeftControl)) //
Input.GetKey / GetButton / GetAxis;
controller.height = newHeight;
Vector3 newPos = new Vector3(transform.position.x, transform.position.y + 0.5f, transform.position.z);
else if (Input.GetKeyUp(KeyCode.LeftControl)) //
Input.GetKeyUp / GetButtonUp / GetAxisUp;
controller.height = oldHeight;
}
}
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.
using UnityEngine;
using System.Collections;
public class Crouch : MonoBehaviour
{
public CharacterController controller;
public float newHeight;
private float oldHeight;
void Start()
{
controller = GetComponent<CharacterController>();
oldHeight = controller.height;
}
void Update()
{
if (Input.GetKey(KeyCode.LeftControl)) //Input.GetKey / GetButton / GetAxis;
{
controller.height = newHeight;
Vector3 newPos = new Vector3(transform.position.x, transform.position.y + 0.5f, transform.position.z);
}
else if (Input.GetKeyUp(KeyCode.LeftControl)) //Input.GetKeyUp / GetButtonUp / GetAxisUp;
{
controller.height = oldHeight;
}
}
}
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.
I used it off of this post here-( Crouch script. ), and fixed all the errors including the vector3 problem. I didn’t see that newPos was there, so i just looked past it.