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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Error CS1022

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

  1. ruweb

    ruweb

    Joined:
    Feb 27, 2020
    Posts:
    6
    I am writing my first game's script, but I got two problems:

    -Assets\scripts\carro.cs(13,1): error CS1022: Type or namespace definition, or end-of-file expected
    -Assets\scripts\carro.cs(46,1): error CS1022: Type or namespace definition, or end-of-file expected

    So, I was searching in some forums and for many people, the problems are the semicolon in lines 13 and 46( I wrote that in blue, so u can see it easier). Does anyone know wich is the problem?

    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 me to fix that, plz?(sorry for grammatical and orthographic errors, I'm Brazilian).
     
  2. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    The '}' and '{' in this part of the code is confusing the compiler. You want to make sure the curly bracket after the class declaration covers the entire class and all of it's functions, not just the variables. Just get rid of the two curly brackets shown here:
    Code (CSharp):
    1. private Rigidbody CarRig;
    2. public int Speed;
    3. public int RotSpeed;
    4. }
    5.  
    6. {
    7. // Start is called before the first frame update
    8. void Start()
     
    ruweb likes this.
  3. HarryWhitelegg02

    HarryWhitelegg02

    Joined:
    Mar 11, 2020
    Posts:
    39
    Your brackets are opening then closing the public class, you just need to delete the bracket below your variables and then delete the last bracket. <3
     
  4. 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);

    }

    }
     
  5. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    Please use code tags to post code examples. When doing so, and having proper indentation, it should be pretty obvious where the error is. As the message says, it is expecting a closing curley bracket somewhere. This means you have more opening { than closing } brackets, but the numbers have to match.

    Each class, method, property, if-statement, for-loop and so, defines a "code-body" or "scope" below is. This scope is marked by a pair of curley brackets such that the content of the class, method, ... is inside of them ie, { ... }. Each such scope also should have its own visual indentation (which makes it easier to find these mistakes).

    In your case, the problem can be fixed by adding one more } at the end of your script. Your class carro has an opening curley bracket, but no closing one. You can see that when matching brackets. Each if statement has an opening one and a closing one, Start() has both as well, Update() also, but the closing bracket of Update() is the last in the script, so your class itself has no closing bracket. Again, when using a proper IDE and code indentation, this should be rather easy to detect, so make sure to use proper indentation levels for each code body :)