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. Dismiss Notice

Question (8,5): error CS8803: Top-level statements must precede namespace and type declarations

Discussion in 'Scripting' started by serabaflo10, Jul 21, 2023.

  1. serabaflo10

    serabaflo10

    Joined:
    Jul 18, 2023
    Posts:
    4
    Hello, im having trouble with this error, on line 8, 5.
    I had a error like this not too long ago but I think that the error is not in the line that it shows so I don't know.
    Here's the code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class moimientoo : MonoBehaviour{}
    6.  
    7.  
    8.     bool jump;
    9.     bool move;
    10.  
    11.     // Start is
    12.     // called before the first frame update
    13.     void Start()
    14.     {
    15.  
    16.     }
    17.  
    18.     // Update is called once per frame  
    19.     void Update()
    20.     {
    21.         if (Input.GetKey("left"))
    22.         {
    23.             gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(-1000 * Time.deltaTime, 0));
    24.         }
    25.  
    26.         if (Input.GetKey("right"))
    27.         {
    28.             gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(1000 * Time.deltaTime, 0));
    29.         }
    30.         if (Input.GetKeyDown("up") && jump)
    31.         {
    32.             gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0, 250f));
    33.         }
    34.     }
    35.  
    36. void OnCollisionEnter2D(Collision2D collision)
    37. {
    38.     if (collision.transform.tag == "zuelo")
    39.     {
    40.         jump = true;
    41.     }
    42. }
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    I'll remove the tag "2D physics" because your post is nothing to do with that. I'll also move your post to the Scripting forum as again, the above is nothing to do with 2D but rather just a typo or misunderstanding of C#.

    Code (CSharp):
    1. public class moimientoo : MonoBehaviour{}
    You don't put the curly braces like that when defining a C# class; they are supposed to encapsulate the contents of the class i.e. your fields, methods etc. Unity creates you this class when you add a script so just add one and have a look at it or follow any tutorial online to see a class.

    I would recommend that you follow some documentation/tutorials on how to define a C# class such as this one: https://www.w3schools.com/cs/cs_classes.php
     
  3. serabaflo10

    serabaflo10

    Joined:
    Jul 18, 2023
    Posts:
    4
    Removing the curly braces just gives me a LOT of errors
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    Because that's an error and I didn't say just remove them and it'd be fine. ;)

    Please just read what I put, create a new script using Unity or look at the document I linked that shows you how a C# class is structured.
     
  5. serabaflo10

    serabaflo10

    Joined:
    Jul 18, 2023
    Posts:
    4
    Yes but I only have one error. When I remove the braces, 8 new errors appear.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    As above, I never said remove them.

    Okay, explicitly:

    Code (CSharp):
    1. public class moimientoo : MonoBehaviour
    2. {
    3.     bool jump;
    4.     bool move;
    5.  
    6.    // other stuff INSIDE the braces...
    7. }
     
    mopthrow and Bunny83 like this.
  7. serabaflo10

    serabaflo10

    Joined:
    Jul 18, 2023
    Posts:
    4
    Thanks! But now im getting the same thing in line 13,5, but, it seems I have the braces
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    Then please go follow some basic C# tutorials because these forums are not really suited for teaching the basics like this.
     
    mopthrow and Bunny83 like this.
  9. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,495
    The error is always either on the line or somewhere before that line that is reported. The compiler reads your code just like you would read a book. From the top left to the right and line by line downwards. An earier mistake might only surface at a later point in time.

    In line 5 you declared a new class type named "moimientoo". However the body of that class is started and ended on the same line since you have the
    {
    and
    }
    right on the same line. That means your class declaration is finished at that point. That means the code in line 8 makes no sense since you try to declare a variable but you're not inside a class / type definition. What you want is this:


    Code (CSharp):
    1.     using System.Collections;
    2.     using System.Collections.Generic;
    3.     using UnityEngine;
    4.    
    5.     public class moimientoo : MonoBehaviour
    6.     {
    7.         bool jump;
    8.         bool move;
    9.    
    10.         // Start is
    11.         // called before the first frame update
    12.         void Start()
    13.         {
    14.    
    15.         }
    16.    
    17.         // Update is called once per frame
    18.         void Update()
    19.         {
    20.             if (Input.GetKey("left"))
    21.             {
    22.                 gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(-1000 * Time.deltaTime, 0));
    23.             }
    24.    
    25.             if (Input.GetKey("right"))
    26.             {
    27.                 gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(1000 * Time.deltaTime, 0));
    28.             }
    29.             if (Input.GetKeyDown("up") && jump)
    30.             {
    31.                 gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0, 250f));
    32.             }
    33.         }
    34.    
    35.         void OnCollisionEnter2D(Collision2D collision)
    36.         {
    37.             if (collision.transform.tag == "zuelo")
    38.             {
    39.                 jump = true;
    40.             }
    41.         }
    42.     }
    Note that you currently set your "jump" variable to true when you start colliding with something. However you never set the variable back to false anywhere. You may want to use OnClooisionStay2D instead to set it to true and set the variable to false when you actually jump. Though that's up to you.
     
  10. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,084
    No. You have nine errors. One of them is hiding the other eight. C# will do that with some errors. In this case you're not using opening/closing curly braces correctly. A class should have a pair the surrounds everything belonging to that class. A method should have a pair that surround everything belonging to the method.

    If you accidentally leave part of your class outside of the class curly braces or part of the method outside of the method curly braces you will get errors like that (it won't always be that specific error).
     
    Last edited: Jul 21, 2023