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

I cant fix the CS8803 problem, Can you help me??

Discussion in 'Getting Started' started by Beckish, May 4, 2023.

  1. 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);
    }
    }
     
  2. Beckish

    Beckish

    Joined:
    May 3, 2023
    Posts:
    4
    (PlayerMotor.cs(15,9): error CS8803: Top-level statements must precede namespace and type declarations.)
     
  3. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    491
    There are so many things wrong with this code you really need to go and do a load of basic C# tutorials before you start to attempt coding a game.

    - Move the first
    }
    to the end of the script.
    - Remove the
    ;
    from
    void Start();
    line
    - Remove the
    ;
    from
    void ProcessMove(Vector2 input);
    line
    - Move the whole of the
    ProcessMove
    so that it is outside the
    Update
    . Look at the
    {
    and
    }
    of the Update method to see where it begins and ends and you should be able to see that your
    ProcessMove
    method is inside
    Update
    at the moment.
    - You never actually call the
    ProcessMove
    method anywhere, so at the moment it doesn't do anything.
    - This line
    if (Physics.Raycast(ray, out hit, 100, ourMovmentMask));
    is not really doing anything as you have a
    ;
    at the end of the line which is effectively saying if you meet the condition do nothing. You also don't have anything that it should be doing afterwards as you don't have anything within any
    {
    }

    - On the
    if (Physics.Raycast...
    you use a variable called
    ourMovmentMask
    yet you have not defined that variable anywhere.
    - In your
    Start
    method you assign values to
    cam
    and
    motor
    yet you have not defined those variables anywhere.
    - In the
    ProcessMove
    method you set up a variable
    movedirection
    but then assign values to
    moveDirection
    . C# is a case-sensitive language. You must type the variables exactly the same otherwise they are understood by the language to be different.

    I suspect that there are even more errors in this script, but at the moment it is not feasible trying to find them as it is clear you do not yet have a grasp of even standard C# coding practices. Go and work through the Unity Learn course work from the very beginning to start with, otherwise you are just going to be coming back here constantly as you run into more and more problems. This may sound harsh but the reality is, if you don't have the fundamentals of C# down then you will not get very far with your game (all of the above problems should have been very easy to find if you know C# and were using an IDE that will highlight your errors).

    Also, one final point:
    - You have not used code tags when posting your code, so we have no line numbers to refer to when giving you answers. This is in the guidelines for the forum (and a link is in my signature) so you should always use code tags to post your script code.
     
  4. RichAllen2023

    RichAllen2023

    Joined:
    Jul 19, 2016
    Posts:
    1,026
    I'm a coding beginner, and even I can tell you there's a LOT wrong with ALL your code.

    Spelling and grammar are all over the place for a start.