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. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    Dismiss Notice

A namespace cannot directly contain members such as fields or methods?

Discussion in 'Getting Started' started by Taek16, Sep 8, 2019.

  1. Taek16

    Taek16

    Joined:
    Sep 8, 2019
    Posts:
    4
    I have just started using Unity and C#, and I can't find what is causing the error message " A namespace cannot directly contain members such as fields or methods" in my code.
    This is my code:

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

    public class PlayerMovement : MonoBehaviour
    {
    public Rigidbody rb;

    }

    void Update()
    {
    if (Input.GetKey("a"))
    {
    rb.AddForce(-50, 0, 0);
    }

    if (Input.GetKey("d"))
    {
    rb.AddForce(50, 0, 0);
    }
    }
     
  2. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    18,916
    In my experience the most common problems for a beginner are using too many opening/closing braces, mixing up where the braces are positioned, forgetting semi-colons, etc. You have one extra closing brace between the rigidbody declaration and the functions. You need to move it to the bottom of the class below the functions where you're missing a closing brace.
     
    Joe-Censored, Taek16 and JoeStrout like this.
  3. Taek16

    Taek16

    Joined:
    Sep 8, 2019
    Posts:
    4
    This fixed it, thank you so much!
     
  4. longjohn1950s

    longjohn1950s

    Joined:
    Jul 13, 2020
    Posts:
    1
    AAAAH IM DONE WITH THIS CRAP I FOLLOWED A DAMN TUTORIAL AND
    I CARBON COPIED IT BUT IT WONT WORK AHHHHH ok sorry but for real it is really annoying
     

    Attached Files:

  5. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Your problem is exactly what Ryiah mentioned above: you're closing the braces for your class too early, leaving the Start and Update methods outside of the class.

    Just move the closing curly brace on line 13 to the end of the file.
     
  6. JORACBARORAC

    JORACBARORAC

    Joined:
    May 17, 2021
    Posts:
    1
    Can you help me find if there's something bad in my code then? I can't find it and it's really frustrating.
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using static UnityEngine.MonoBehaviour;
    Public class PlayerMovement : MonoBehaviour
    {
    [SerializeField] private float speed = 7f;
    private CharacterController charController;

    // Start is called before the first frame update
    void Start()
    {
    charController = GetComponent<CharacterController>();
    }

    // Update is called once per frame
    void Update()
    {
    //Get WASD input for player
    float horizontal = input.GetAxis("Horizontal");
    float vertical = input.GetAxis("Vertical");
    //Move player based on WASD input
    Vector3 movement = Vector3.forward * vertical + vector3.right * horizontal;
    charController.Move(movement * Time.deltatime);


    //Get mouse input
    //Get player to look using mouse input
    }
    }

    It gives me the same error: error CS0116: A namespace cannot directly contain members such as fields or methods
    And it still brings up errors if I change the MonoBehaviour class to private
     
  7. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    18,916
    You've incorrectly capitalized the keyword "public". That's not the only problem though.

    You need to capitalize Input.

    Same with the third Vector3 in this instruction.

    C# is a very picky language when it comes to formatting and capitalization is an easy problem to stumble into but the compiler is too dumb to notice when it's happening so it's going to be up to the developer to spot it when it happens.

    A proper code editor like Visual Studio can assist with catching errors like these and when it does it will place a red squiggly line below it.
     
    Last edited: May 18, 2021
  8. ivan_kolegov

    ivan_kolegov

    Joined:
    Jan 25, 2023
    Posts:
    1
    Помогите мне пожалуйста я новичок и мне выдаёт эту ошибку. Я не могу
    понять как её решить. Вот код.
    Система.Коллекции;
    использование System.Coиспользование лекции. Родовой;
    using UnityEngine;
    public float speed;
    public class NewBehaviourScript : MonoBehaviour
    {
    }