Search Unity

Error CS1002 Expected

Discussion in 'Scripting' started by ThreeCents, Jul 12, 2018.

Thread Status:
Not open for further replies.
  1. ThreeCents

    ThreeCents

    Joined:
    Jul 12, 2018
    Posts:
    32
    I am starting with Unity and C# and I got this error : Assets/PlayerControls.cs(39,3): error CS1002: ; expected
    I can't find it :/ .

    Here's my code ;

    sing System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class PlayerControls : MonoBehaviour {

    public KeyCode moveUp = KeyCode.W;
    public Keycode moveDown = Keycode.S;
    public float speed = 10.0f;
    public float boundY = 2.25f;
    private Rigidbody2D rb2d;

    void Start() {
    rb2d = GetComponent<Rigidbody2D>();

    }
    void Update() {
    var vel = rb2d.velocity;
    if (Input.GetKey(moveUp)) {
    vel.y = speed;
    }
    else if (Input.GetKey(moveDown)) {
    vel.y = -speed;
    }
    else {
    vel.y = 0;

    }
    rb2d.velocity = vel;

    var pos = transform.position;
    if (pos.y > boundY) {
    pos.y = boundY;

    }
    else if (pos.y < -boundY) {
    pos.y = -boundY

    }
    transform.position = pos;

    }

    }
     
    honkonenaxel and Max_tallboi like this.
  2. Please use the code tags here if you post code.
    And the quoted lines above should be:
    Code (CSharp):
    1. else if (pos.y < -boundY) {
    2.   pos.y = -boundY;
    3. }
    (I guess the "pos.y = -boundY" is the 39th line)
     
    Mayhem6489 and NicolasIT like this.
  3. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,092
    You should read the error message carefully.

    Assets/PlayerControls.cs(39,3): error CS1002: ; expected

    Script: PlayerControls.cs
    line number 39 (usually in an IDE such as visual studio line numbers can be found on the left)
    a semicolon ; was expected.

    which
    pos.y = -boundY
    indeed you forgot a ";" like LurkingNinjaDev already said.
     
  4. ThreeCents

    ThreeCents

    Joined:
    Jul 12, 2018
    Posts:
    32
    Thanks ! Sorry I am starting with unity and I didn't knew that (39,3) was a line ^^''
     
    Charlula likes this.
  5. PerseusX

    PerseusX

    Joined:
    Jan 20, 2018
    Posts:
    1
    on this subject please help me im getting this: Assets\Door.cs(8,12): error CS1002: ;


    this is my script:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Door : MonoBehaviour
    {
    Animator animator;
    bool Door Open

    void Start()
    {
    Door Open = false;
    animator = GetComponent<Animator>();
    }

    void OnTriggerEnter(Collider col)
    {
    if(Col.gameObject.tag == "Player")
    {
    Door Open = true;
    Doors ("Open");
    }
    }

    void OnTriggerExit(Collider col)
    {
    if(doorOpen)
    {
    doorOpen = false;
    Doors ("Close");
    }
    }

    void Doors(string direcrion)
    {
    animator.SetTrigger(direction);
    }
    }
     
  6. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,092
    Please use the code tags, they are in the toolbar when writing a post... they make reading code a lot more easier.
    But as for you as well, line 8 is your error
    “bool Door Open” probably, you forgot the semicolon ‘;’ there.
     
  7. Phenom_07

    Phenom_07

    Joined:
    Sep 28, 2019
    Posts:
    6
    getting error ; expected line(20,28)



    Code (Csharp):
    1.  
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. public class NewBehaviourScript : MonoBehaviour
    8. {
    9.     int DamageAmount=5;
    10.     float TargetDistance;
    11.     float AllowedRange=20;
    12.  
    13.     // Start is called before the first frame update
    14.  
    15.  
    16.     // Update is called once per frame
    17.      void  Update ()
    18.      {
    19.     if(Input.GetButtonDown("Fire1"))
    20.      {
    21.  
    22.             var RaycastHit Shot;
    23.             if (Physics.Raycast (transform.Transformposition, transform.TransformDirection(Vector3.forward), Shot)) {
    24.                 TargetDistance = Shot.distance;
    25.                     if (TargetDistance < AllowedRange) {
    26.                         Shot.transform.SendMessage("DeductPoints", DamageAmount);
    27.                     }
    28.                 }
    29.             }
    30.         }
    31. }
    32.  
     
    Last edited: Sep 29, 2019
  8. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,092
    Spiketail likes this.
  9. Phenom_07

    Phenom_07

    Joined:
    Sep 28, 2019
    Posts:
    6
    updated my code kindly help
     
  10. Phenom_07

    Phenom_07

    Joined:
    Sep 28, 2019
    Posts:
    6
  11. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,092
    transform.Transformposition
    is not a valid property. That could be an issue. Unless you've made an extension for it. But I doubt that.
    use
    transform.position


    https://docs.unity3d.com/ScriptReference/Transform.html

    Your IDE should be able to tell you that the property Transformposition doesn't exist.

    The Shot should also be 'out Shot' as parameter. Since it is an out parameter of
    Physics.Raycast
    API
    Also try and read about the
    Raycasting
    API
    https://docs.unity3d.com/ScriptReference/Physics.Raycast.html

    Also next time try to create a new thread for this instead of hijacking someone elses :p
     
    Last edited: Sep 29, 2019
  12. Phenom_07

    Phenom_07

    Joined:
    Sep 28, 2019
    Posts:
    6
    still same error is occuring

    Code (csharp):
    1.  
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. public class HandGun : MonoBehaviour
    8. {
    9.     int DamageAmount=5;
    10.     float TargetDistance;
    11.     float AllowedRange=20;
    12.  
    13.     // Start is called before the first frame update
    14.    void Start()
    15.    {
    16.      
    17.    }
    18.  
    19.     // Update is called once per frame
    20.      void  Update ()
    21.      {
    22.     if(Input.GetButtonDown("Fire1"))
    23.      {
    24.  
    25.             float RaycastHit Shot ;
    26.             if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), Shot)) {
    27.                 TargetDistance = Shot.distance;
    28.                     if (TargetDistance < AllowedRange) {
    29.                         Shot.transform.SendMessage("DeductPoints", DamageAmount);
    30.                     }
    31.                 }
    32.             }
    33.         }
    34. }
    35.  
     
  13. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,092
    I've edited my last post.

    if ( Physics.Raycast ( transform.position, transform.TransformDirection(Vector3.forward), out Shot) )
     
  14. Phenom_07

    Phenom_07

    Joined:
    Sep 28, 2019
    Posts:
    6
    not working sir,
    its showing
    Assets\scripts\HandGun.cs(23,30): error CS1002: ; expected
     
  15. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,092
    You had
     float RaycastHit Shot;
    <- Declaring the variable as float and RaycastHit. Thats not possible.

    And still the out parameter not set as 'out'.

    so, try this:
    Code (CSharp):
    1. private void Update()
    2. {
    3.     if(Input.GetButtonDown("Fire1"))
    4.     {
    5.         RaycastHit Shot;
    6.         if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), out Shot))
    7.         {
    8.             TargetDistance = Shot.distance;
    9.             if (TargetDistance < AllowedRange)
    10.             {
    11.                 Shot.transform.SendMessage("DeductPoints", DamageAmount);
    12.             }
    13.         }
    14.     }
    15. }
     
  16. Phenom_07

    Phenom_07

    Joined:
    Sep 28, 2019
    Posts:
    6
    getting error

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class crossanimate : MonoBehaviour
    7. {
    8.     GameObject UpCurs;
    9.     GameObject DownCurs;
    10.     GameObject LeftCurs;
    11.     GameObject RightCurs;
    12.  
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.        
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.         if (Input.GetButtonDown("Fire1")) {
    24.         UpCurs.GetComponent<Animator>().enabled=true;
    25.         DownCurs.GetComponent<Animator>().enabled=true;
    26.         LeftCurs.GetComponent<Animator>().enabled=true;
    27.         RightCurs.GetComponent<Animator>().enabled=true;
    28.         WaitingAnim();
    29.     }
    30.        
    31.     }
    32.     void  WaitingAnim (){
    33.     return yield  new WaitForSeconds(0.1f);
    34.     UpCurs.GetComponent<Animator>().enabled=false;
    35.     DownCurs.GetComponent<Animator>().enabled=false;
    36.     LeftCurs.GetComponent<Animator>().enabled=false;
    37.     RightCurs.GetComponent<Animator>().enabled=false;
    38. }
    39. }
    40.  
     
  17. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,092
    Again please try and create a new thread for this different problem instead of hijacking other's thread with a different problem. Each C# compiler error is different and in different context in every script thus should be a different thread unless related to the same setup.

    Code (CSharp):
    1. void  WaitingAnim ()
    2. {
    3.     return yield  new WaitForSeconds(0.1f);
    4.     UpCurs.GetComponent<Animator>().enabled=false;
    5.     DownCurs.GetComponent<Animator>().enabled=false;
    6.     LeftCurs.GetComponent<Animator>().enabled=false;
    7.     RightCurs.GetComponent<Animator>().enabled=false;
    8. }
    This time i'm going to give you a hint where the problem is.

    return yield new WaitForSeconds(0.1f);
    and also the method being a type
    void

    It has to do with Coroutines.

    https://docs.unity3d.com/ScriptReference/Coroutine.html

    The compiler is telling you, you can't run
    return yield new WaitForSeconds(time)

    Have a look at the script reference and check out how coroutines work. There are multiple things wrong with just that method.
     
  18. Synpse

    Synpse

    Joined:
    Mar 19, 2018
    Posts:
    20
    You forgot a ";" here:

    Code (CSharp):
    1. pos.y = -boundY
     
  19. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,092
    That has already been said... twice even
     
  20. Josh_Johansen384

    Josh_Johansen384

    Joined:
    Oct 5, 2019
    Posts:
    1
    help i get the message that a semicolon is expected and i added it but it still says i am wrong help. here is my code
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    var TheDamage : int = 50;
    var Distance : float;
    function Update()
    {
    if input.GetButtonDown("Fire1"))
    {
    var hit : RaycastHit;
    if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), hit))
    {
    Distance = hit.distance;
    hit.transform.SendMessage("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReceiver);
    }
    }
    }
     
  21. Please
    - open your own thread, do not hijack other people's thread with different questions
    - please use the CODE tags as you could see above if you would have made the effort to do so (https://forum.unity.com/threads/using-code-tags-properly.143875/)
    - and finally
    -- where is your class definition?
    -- Why do you think this would work? var TheDamage : int = 50;

    In C# the proper format is <scope> <type> <name>[ = <initial value>];
    Which means in this case: private int TheDamage = 50;

    Quickly clean up your mess, it's something like this:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. class Whatever : MonoBehaviour {
    6.     private int TheDamage = 50;
    7.     private float Distance = 0f;
    8.     void Update()
    9.     {
    10.         if (Input.GetButtonDown("Fire1"))
    11.         {
    12.             RaycastHit hit;
    13.             if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit))
    14.             {
    15.                 Distance = hit.distance;
    16.                 hit.transform.SendMessage("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReceiver);
    17.             }
    18.         }
    19.     }
    20. }
    Please, make sure you learn how to write basic C# scripts. It is not Javascript.
     
  22. Willads02

    Willads02

    Joined:
    Mar 22, 2020
    Posts:
    1
    It says ''Assets\moves.cs(52,35): error CS1002: ; expected''. Can someone help me?

    Heres the script:


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

    public class Spilkarekter4 : MonoBehaviour
    {
    public float speed = 1.0f;
    public string axisName = "Horizontal";
    private Animator anim;
    public string jumpButton = "Fire1";
    public float jumpPower = 10.0f;
    public float minJumpDelay = 0.5f;
    public Transform[] groundChecks;
    private float jumpTime = 0.0f;
    private Transform currentPlatform = null;
    private Vector3 lastPlatformPosition = Vector3.zero;
    private Vector3 currentPlatformDelta = Vector3.zero;
    public float groundRadius = 0.2f;
    public LayerMask whatIsGround;
    bool doubleJump = false;

    // Use this for initialization
    void Start()
    {
    anim = gameObject.GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
    //Left and right movement
    anim.SetFloat("Speed", Input.GetAxis(axisName));
    /*if(Input.GetAxis(axisName) < -0.1)
    {
    Vector3 theScale = transform.localScale;
    theScale.x *= -1;
    transform.localScale = theScale;
    }
    else if(Input.GetAxis(axisName) > 0.1)
    {
    Vector3 theScale = transform.localScale;
    theScale.x *= 1;
    transform.localScale = theScale;
    }*/
    transform.position += transform.right * Input.GetAxis(axisName) * speed * Time.deltaTime;

    //Jump logic
    bool grounded = false;
    foreach (Transform groundCheck in groundChecks)
    {
    grounded |= Rigidbody 2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
    }
    anim.SetBool("Grounded", grounded);
    if (jumpTime > 0)
    {
    jumpTime -= Time.deltaTime;
    }
    if (Input.GetButtonDown(jumpButton) && anim.GetBool("Grounded"))
    {
    anim.SetBool("Jump", true);
    rigidbody2D.AddForce(transform.up * jumpPower);
    jumpTime = minJumpDelay;
    }
    if (anim.GetBool("Grounded") && jumpTime <= 0)
    {
    anim.SetBool("Jump", false);
    }



    //if (anim.GetBool("Grounded")) {
    //Moving platform logic
    //Check what platform we are on
    List<Transform> platforms = new List<Transform>();
    bool onSamePlatform = false;
    foreach (Transform groundCheck in groundChecks)
    {
    RaycastHit2D hit = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
    if (hit.transform != null)
    {
    platforms.Add(hit.transform);
    if (currentPlatform == hit.transform)
    {
    onSamePlatform = true;
    }
    }
    }


    if (!onSamePlatform)
    {
    foreach (Transform platform in platforms)
    {
    currentPlatform = platform;
    lastPlatformPosition = currentPlatform.position;
    }
    }

    if (currentPlatform != null)
    {
    //Determine how far platform has moved
    currentPlatformDelta = currentPlatform.position - lastPlatformPosition;

    lastPlatformPosition = currentPlatform.position;
    }
    //}
    }

    void LateUpdate()
    {
    if (anim.GetBool("Grounded"))
    {
    if (currentPlatform != null)
    {
    //Move with the platform
    transform.position += currentPlatformDelta;
    }
    }
    }
    }
     
  23. Tyrceratops

    Tyrceratops

    Joined:
    Mar 30, 2020
    Posts:
    2
    Hello, i had this error with my code. Some help? Assets\PlayerController.cs(30,67): error CS1002: ; expected
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour {
    6.  
    7.     public float h = 2f;
    8.     public bool grounded;
    9.  
    10.     private Rigidbody2D body;
    11.     private Animator anim;
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         body = GetComponent<Rigidbody2D>();
    16.         anim = GetComponent<Animator>();
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update(){
    21.         anim.SetFloat("Speed", Mathf.Abs(body.velocity.x));
    22.         anim.SetBool("Grounded", grounded);
    23.     }
    24.  
    25.     void fixedUpdate(){
    26.            float direccion = Input.GetAxis("Horizontal");
    27.         body.velocity = new Vector3(direccion * h, body.velocity.y, transform.position.z );
    28.  
    29.         float limitedSpeed = Mathf.Clamp(body.velocity.x -maxSpeed, maxSpeed);
    30.         body.velocity = new Vector2(limitedSpeed, body.velocity.y)
    31.  
    32.         Debug.Log(body.velocity.x);
    33.     }
    34. }
    35.  
     
  24. bleduc

    bleduc

    Joined:
    Apr 1, 2020
    Posts:
    1
    My is error cs1002(40,25) the line is void SetcountText ()
     
  25. MG_Deren

    MG_Deren

    Joined:
    Apr 18, 2020
    Posts:
    2
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerCombat : MonoBehaviour
    6. {
    7.  
    8.     public Animator animator;
    9.  
    10.    // Update is called once per frame
    11.    void Update()
    12.    {
    13.         if (Input.GetKeyDown(KeyCode.W))
    14.         {
    15.             Attack();
    16.         }
    17.    }
    18.  
    19.    void Attack()
    20.    {
    21.        // Play an attack animation
    22.        animator.SetTrigger("Attack")
    23.    }
     
  26. MG_Deren

    MG_Deren

    Joined:
    Apr 18, 2020
    Posts:
    2
    Did i do something wrong?
     
  27. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,157
    Like the error says there is a missing semi-colon at the end of line 22.
     
  28. EirikDF

    EirikDF

    Joined:
    Apr 22, 2020
    Posts:
    1
    The name "z" does not exist in the current context
    What do i do. watch a video on it, and i did the exactly same thing as him
     
  29. alejodsantana

    alejodsantana

    Joined:
    Apr 25, 2020
    Posts:
    1
    hola necesito ayuda me venta este error, podrian ayudarme
    hola necesito ayuda me sale este error, podrías ayudarme
    Assets / player.cs (13,42): error CS1002:; esperado

    usando System.Collections;
    usando System.Collections.Generic;
    usando UnityEngine;

    jugador de clase pública: MonoBehaviour {

    velocidad de flotación pública = 2f;

    Rigidbody2D privado rbd2d;

    // Se llama al inicio antes de la primera actualización del marco
    Inicio nulo () {
    rb2d = GetComponent <Rigidbody2D> ()
    }

    // Actualización se llama una vez por cuadro
    Actualización nula () {

    }

    void FixedUpdate () {

    flotante h = Imput.GetAxis ("horizontal");



    }

    }
     
Thread Status:
Not open for further replies.