Search Unity

Error CS1003 Syntax error "{" expected

Discussion in 'Scripting' started by MohammadNakouzi, Jan 17, 2021.

  1. MohammadNakouzi

    MohammadNakouzi

    Joined:
    Dec 27, 2020
    Posts:
    3
    I wrote this code before and didn't get this error.
    Here is the code :

    ausing System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class PlayerMovement : MonoBehaviour
    {
    public CharacterController controller;

    public float speed = 12f;
    public float gravity = -9.81f;
    public float jumpHeight = 3;

    public Transform groundCheck;
    public float groundDistance = 0.4f;
    public LayerMask groundMask;

    Vector3 velocity;
    bool isGrounded;

    // Update is called once per frame
    void Update()
    {

    isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

    if (isGrounded && velocity.y < 0)
    {
    velocity.y = -2f;
    }

    float x = Input.GetAxis("Horizontal");
    float z = Input.GetAxis("Vertical");

    Vector3 move = transform.right * x + transform.forward * z;

    if (Input.GetButtonDown("Jump") && isGrounded) ;
    {
    velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
    }

    controller.Move(move * speed * Time.deltaTime);

    velocity.y += gravity * Time.deltaTime;
    controller.Move(velocity * Time.deltaTime);
    }
    }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Bunny83 and MohammadNakouzi like this.
  3. MohammadNakouzi

    MohammadNakouzi

    Joined:
    Dec 27, 2020
    Posts:
    3
    Nvm found it out
     
  4. MohammadNakouzi

    MohammadNakouzi

    Joined:
    Dec 27, 2020
    Posts:
    3
  5. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,990
    You also have a semicolon after this line which does not cause a syntax error but is most likely not wanted:

    Code (CSharp):
    1. if (Input.GetButtonDown("Jump") && isGrounded) ;
    Due to this semicolon the following block is executed regardless of the if statement.