Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Errors 1022 and 1002

Discussion in 'Scripting' started by ruweb, Mar 12, 2020.

  1. ruweb

    ruweb

    Joined:
    Feb 27, 2020
    Posts:
    6
    I started writing my first game's script today, but I got two problems:
    -error CS1002: ; expected
    -error CS1022: ; Type or namespace definition, or end-of-file expected

    The script is:

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

    public class carro : MonoBehaviour{

    private Rigidbody CarRig;
    public int Speed;
    public int RotSpeed


    }
    {
    // Start is called before the first frame update
    void Start()
    {
    CarRig = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update()
    {
    if (input.GetKey(Keycode.W))
    {
    CarRig.AddForce(transform.forward * Speed * Time.DeltaTime, ForceMode.Impulse);
    }

    if (input.GetKey(Keycode.S))
    {
    CarRig.AddForce(transform.forward * -Speed * Time.DeltaTime, ForceMode.Impulse);
    }

    if (Input.GetKey(Keycode.A))
    {
    transform.Rotate(0, -RotSpeed, 0);

    }

    if (Input.GetKey(Keycode.D))
    {
    transform.Rotate(0, RotSpeed, 0);

    }

    }
    }


    Can someone help meto fix that, plz? (sorry for gramatical and ortographic errors, I am Brazilian)
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,835
    Please use code tags.

    Looks like you forgot to put a semicolon at the end of the line "public int RotSpeed". I suspect your curly braces {} are also messed up right after that, and perhaps at other places, too.
     
    ruweb and Yoreki like this.
  3. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    To add to the already correct answer of Antistone, you may want to look up some naming conventions to make your code more uniform to already preexisting code, as well as the code of other programmers.

    As a rule of thumb, you want to write variable names using camelCase (starting with a lower letter and capitalizing each first letter of a new word). For class names, property names and method names you then use UpperCamelCase (same with first letter capitalized). So mostly the opposite of what you are currently doing. There are some other conventions, but those are the most common ones.
    That said, it's also generally a good idea to chose english identifiers (names). All preexisting code is in english anyways and if you ever need help and post it online, you will want others to understand your code. Debugging code you didnt even write yourself becomes a nightmare if you cant even make sense of the involved variables.
    For some (me at least) it's even confusing to read idenfitiers in their own language. It sort of feels like you are reading a mixture of two languages, or a sentence in which suddenly there are a couple words of a different language.

    These are just suggestions, but if you are learning to programm i would highly recommend getting used to it from the beginning :)
     
    ruweb likes this.
  4. ruweb

    ruweb

    Joined:
    Feb 27, 2020
    Posts:
    6
    thanks dude
     
  5. ruweb

    ruweb

    Joined:
    Feb 27, 2020
    Posts:
    6
    Thanks for the help. About the translation, I'm sorry... i was working in this project with a friend and he doesn't speak English(neither, so we weren't able to write in English.
     
  6. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    No need to be sorry. I just wanted to mention it before you get into a bad habit. If you are not confident enough in your english skills, it's fine to use your own language. Just be aware that, should the code ever be read by other/international people (for example on forums), it will be inconvenient for them. For smaller scripts such as these it's definitely not a problem (use code tags to post code tho), but for longer ones it would be.
    Either way, take a look at naming conventions.
    They are independant of the language you use and exist to help you in the end.

    Happy coding!
     
    ruweb likes this.
  7. ruweb

    ruweb

    Joined:
    Feb 27, 2020
    Posts:
    6
    Thanks for the help man, but it still doesn't working:

    -Assets\scripts\carro.cs(45,7): error CS1513: } expected

    I've tried to fix that, but I can't. Can you help me, plz?

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

    public class carro : MonoBehaviour
    {

    private Rigidbody CarRig;
    public int Speed;
    public int RotSpeed;



    // Start is called before the first frame update
    void Start()
    {
    CarRig = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update()
    {
    if (input.GetKey(Keycode.W))
    {
    CarRig.AddForce(transform.forward * Speed * Time.DeltaTime, ForceMode.Impulse);
    }

    if (input.GetKey(Keycode.S))
    {
    CarRig.AddForce(transform.forward * -Speed * Time.DeltaTime, ForceMode.Impulse);
    }

    if (Input.GetKey(Keycode.A))
    {
    transform.Rotate(0, -RotSpeed, 0);

    }

    if (Input.GetKey(Keycode.D))
    {
    transform.Rotate(0, RotSpeed, 0);

    }

    }
     
  8. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    I just realised this is a duplicate of https://forum.unity.com/threads/error-cs1022.845665/ .
    I already answered your current problem there.
    Please dont post the same question in multiple threads at the same time.

    (Also people asked you to do so multiple times already. Please use code tags to post code examples! It improves readability by a lot. Take a look at the sticky post about code tags in this scripting subforum in case you are unsure how to use code tags.)