简体   繁体   中英

Error “A namespace cannot directly contain members such as fields or methods” what's wrong with my code? [on hold]

I have been trying to make all these errors go away for a few hours now and i have tried a lot of methods but somehow none are working, i am new to unity and visual studios.Pls help me solve this.

I have tried a lot of methods of putting brackets at different places.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    namespace e
    {
        public class Movement2D : MonoBehaviour
        {

        }



        public float movespeed = 5f;
        void Start()
        {

        }
        void Update()
        {
            object p = Jump();
            Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
            transform.position += movement * Time.deltaTime * moveSpeed;
        }
        void Jump()
        {


            if (Input.GetButtonDown("Jump"))
            {
               gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, 5f), ForceMode2D.Impulse);
            }
        }
    }

I think that the problem is the placement of my brackets, but i do not know where to place them. It just keeps on telling me that "A Namespace cannot directly contain members such as fields or methods.

If my comment wasn't clear, code should look like this.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace e
{
    public class Movement2D : MonoBehaviour
    {
        public float movespeed = 5f;

        void Start()
        {

        }

        void Update()
        {
            object p = Jump();
            Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
            transform.position += movement * Time.deltaTime * moveSpeed;
        }

        void Jump()
        {
            if (Input.GetButtonDown("Jump"))
            {
                gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, 5f), ForceMode2D.Impulse);
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM