Search Unity

Question how to fix error code cs8803?

Discussion in 'Getting Started' started by BubbaHuds, Jul 3, 2022.

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

    BubbaHuds

    Joined:
    Jul 3, 2022
    Posts:
    4
    Assets\playerScripts.cs(21,1): error CS8803: Top-level statements must precede namespace and type declarations. Do not know what this means.
     
  2. BubbaHuds

    BubbaHuds

    Joined:
    Jul 3, 2022
    Posts:
    4
    That Should be my code
     

    Attached Files:

  3. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,205
    You need to move those variable definitions into the class.
     
  4. Strongystile

    Strongystile

    Joined:
    Aug 15, 2022
    Posts:
    3
    like im a SUPER N00B at this so what does that mean?
     
  5. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Why do you ask, are you getting the same error?
     
  6. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
    JeffDUnity3D likes this.
  7. theCRAZYmonster

    theCRAZYmonster

    Joined:
    Jan 16, 2023
    Posts:
    1
    im having the same issue and this is my file
     

    Attached Files:

  8. RichAllen2023

    RichAllen2023

    Joined:
    Jul 19, 2016
    Posts:
    1,026
    I had a similar problem last week, but it's probably because due to printer issues, I couldn't print out the code to type it in by hand, I copied and pasted it and it still didn't work and threw up errors.
     
  9. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,445
    When you're just starting out, the purpose of curly braces { } is not very clear. They seem to be scattered all over the place.

    The purpose of curly braces is to contain stuff that's related. Name of thing, open brace, stuff related to thing, close brace.

    Your code starts a class and gives it a name: public class PlayerMovement
    Your code opens a brace: {
    Your code includes related stuff: void Start() { }
    Your code includes more related stuff: void Update() { }
    Your code closes a brace: }

    Your compiler is happy, it has read everything it needs to read, because every brace that was opened is now closed.

    Then your code includes a whole bunch of other stuff, more open braces, more stuff, more close braces, but nothing to name the thing or explain what it's for. The compiler gets really confused about this jumble of extra stuff swept at the bottom of the file that doesn't say what its name is, or why it's there.
     
  10. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,445
    It's considered bad manners to reply to someone else's thread on a different topic. You should start your own if you have a problem with your code.

    Unfortunately, your code is very short yet filled with errors coming from not understanding C# language syntax.

    public class PlayerMovement : movement
    -- probably no such class called 'movement', probably mean MonoBehaviour here, and watch the capital letters.

    All of the following stuff are statements, and statements can only be inside a method. Missing something like
    void Update() {
    and
    }
    wrapping it.

    Code (csharp):
    1. Vector2 movement = Vector2.zero;
    2. // ... (skipping) ...
    3. movement = movement + (Vector2)(transform.position);
    4.  
    And no idea what you're trying to do with the following here. Copied from some other language, poorly.

    Code (csharp):
    1. global::System p = Objectrigidbody2D.MovePosition(movement);
    I suggest you look for some super-basic tutorials that don't assume you know C# already, and get a better understanding of classes, methods, statements, and the C# language in general. Then other tutorials will make more sense and you can fix your own mistakes.
     
  11. WindTheHedgeho6

    WindTheHedgeho6

    Joined:
    Jan 29, 2023
    Posts:
    1
    Sorry for being new
     
  12. RichAllen2023

    RichAllen2023

    Joined:
    Jul 19, 2016
    Posts:
    1,026
    With respect mate, I'm not very experienced with C# and even I can tell you what's wrong, the code's riddled with bad spelling errors and general typos.

    Such as mistakes with upper and lower case.
     
  13. xplac1

    xplac1

    Joined:
    Feb 10, 2023
    Posts:
    1
    how do you fix this error
     
  14. quandaledinglegames

    quandaledinglegames

    Joined:
    Dec 19, 2022
    Posts:
    1
    Im having the same problem and im doing create with code lesson 3.1 step 5 pls halp (im new to threads forgive me if i screw up)
     
  15. Beckish

    Beckish

    Joined:
    May 3, 2023
    Posts:
    4
    How to fix this error i don't understand all thise codes, My error is litterly PlayerMotor.cs(15,9): error CS8803: Top-level statements must precede namespace and type declarations.
     
  16. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,445
    @Beckish Not sure why you thought replying to somebody else's old thread would help. And you didn't show any code.

    But I will try to read your mind through magical means. You have stuff that is only appropriate in the purple areas or green areas, stuffed into the orange area. The compiler doesn't know what to do with it, unless you structure things cleanly.

     
  17. Beckish

    Beckish

    Joined:
    May 3, 2023
    Posts:
    4
    Im not sure how this works but i trying to code movemeny in unity, Its my first time, Here is my code


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

    [RequireComponent(typeof(PlayerMotor))]
    public class PlayerMotor : MonoBehaviour
    {
    private CharacterController Controller;
    private Vector3 PlayerVelocity;
    private bool isGrounded;
    public float speed = 5f;
    }
    // Start is called before the first frame update
    void Start();
    {
    cam = Camera.main;
    motor = GetComponent<PlayerMotor>();
    }
    // Update is called once per frame
    void Update()
    {
    if (Input.GetMouseButtonDown(0))
    {
    Ray ray = cam.ScreenPointToRay(Input.mousePosition);
    RaycastHit hit;
    if (Physics.Raycast(ray, out hit, 100, ourMovmentMask));

    }
    Debug.Log("we hit " + hit.collider.name + " " + hit.point);
    // move player to what we hit

    // stop focusing any objects

    //recive the inputs for our InputManager.cs and apply them to our character controller.
    void ProcessMove(Vector2 input);
    {
    Vector3 movedirection = Vector3.zero;
    moveDirection.x = input.x;
    moveDirection.y = input.y;
    controller.Move(transform.TransformDirection(moveDirection) *speed* Time.deltaTime);
    PlayerVelocity.y += gravity * Time.deltaTime;
    Controller.Move(PlayerVelocity * Time.deltaTime);
    Debug.Log(PlayerVelocity.y);
    }
    }
     
  18. CoopaTroopa2012

    CoopaTroopa2012

    Joined:
    Aug 3, 2023
    Posts:
    1
    Im New to this coding thing i just started scripting and i got this error
    Assets\LogicScript.cs(15,1): error CS8803: Top-level statements must precede namespace and type declarations.
    im stumped and i cant get over this error please someone help me. Thank you and bye

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using UnityEngine.SceneManagement;

    public class LogicScpirt : MonoBehaviour
    {
    public int playerScore;
    public Text scoreText;
    }

    public void restartGame;
    {

    SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }
    that is my script btw
     
    Last edited: Aug 3, 2023
  19. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    490
    You just made elementary errors, your code should look like this.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.SceneManagement;
    6.  
    7. public class LogicScpirt : MonoBehaviour
    8. {
    9.     public int playerScore;
    10.     public Text scoreText;
    11.  
    12.     public void restartGame()
    13.     {
    14.         SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    15.     }
    16. }
    Start your Unity adventure here https://learn.unity.com/pathway/junior-programmer.
     
  20. Herbooo

    Herbooo

    Joined:
    Jan 3, 2023
    Posts:
    1
    i am getting a similar error to some people here with "
    Assets\Scripts\HealthManagement.cs(21,1): error CS1022: Type or namespace definition, or end-of-file expected"
    the code this error is occurring on is as follows
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class HealthManagement : MonoBehaviour
    6. {
    7.  
    8.     void Start()
    9.     {
    10.         public float totalHP = 1000;
    11.     }
    12.  
    13.     void Update()
    14.     {
    15.         print("current HP is", totalHP);
    16.         if(totalHP < 1)
    17.         {
    18.             print("ded");
    19.         }
    20.     }
    21. }
     
  21. Chubzdoomer

    Chubzdoomer

    Joined:
    Sep 27, 2014
    Posts:
    132
    One problem I see right away is that you're trying to create a public variable inside of a method, which isn't allowed. Variables created inside methods are only visible/accessible from within those methods.

    Move that line of code outside of the Start() method, so that the "public" access modifier is allowed and the totalHP variable can be seen/accessed by the Update() method as well as any other method inside that class.

    https://www.tutorialsteacher.com/articles/variable-scopes-in-csharp

    I also think your print line should be rewritten as follows, with + instead of , between the string and variable:
    print("current HP is" + totalHP);
     
    Last edited: Aug 11, 2023
Thread Status:
Not open for further replies.