how to fix error code cs8803?

Assets\playerScripts.cs(21,1): error CS8803: Top-level statements must precede namespace and type declarations. Do not know what this means.

That Should be my code

8249823–1079469–playerScripts.cs (880 Bytes)

You need to move those variable definitions into the class.

4 Likes

like im a SUPER N00B at this so what does that mean?

Why do you ask, are you getting the same error?

A tour of C# - The building blocks of C# programs | Microsoft Docs

1 Like

im having the same issue and this is my file

8734029–1182309–PlayerMovement.cs (988 Bytes)

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.

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.

1 Like

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.

Vector2 movement = Vector2.zero;
// ... (skipping) ...
movement = movement + (Vector2)(transform.position);

And no idea what you’re trying to do with the following here. Copied from some other language, poorly.

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.

Sorry for being new

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.

how do you fix this error

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)

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.

@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.

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();
}
// 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);
}
}

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

You just made elementary errors, your code should look like this.

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);
    }
}

Start your Unity adventure here https://learn.unity.com/pathway/junior-programmer.

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

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

public class HealthManagement : MonoBehaviour
{

    void Start()
    {
        public float totalHP = 1000;
    }

    void Update()
    {
        print("current HP is", totalHP);
        if(totalHP < 1)
        {
            print("ded");
        }
    }
}