two weird namespace errors

I have like 2 errors from unity that visual studio dont show and I dont know what is means. Please help me I am an idiot.

script I used:

`using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Security.Cryptography;
using System.Threading;
using UnityEngine;

public float movement = 5f;

public class movement : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
    Vector3 movement = new Vector3(Input.getAxis("Horizontal"), 0f, 0f);
    transform.position += movement * Time.deltaTime*movement;
}

}
void jump()
{
if (Input.GetButtonDown(“Jump”))
{
gameObject.GetComponent().addForce(new Vector2(0f, 5f), forceMode2D.Impulse);
};
}`
errors from unity:Assets\movement.cs(10,14): error CS0116: A namespace cannot directly contain members such as fields or methods,
Assets\movement.cs(28,6): error CS0116: A namespace cannot directly contain members such as fields or methods

Hi!

Try next time to format code properly because it will reduce response time since it’s hard to understand what is happening in your code. If I didn’t make any mistake organizing your code then it looks like this:

using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Security.Cryptography;
using System.Threading;
using UnityEngine;
public float movement = 5f;

public class movement : MonoBehaviour
{ // Start is called before the first frame update 

    void Start()
    {
    } // Update is called once per frame void 
    void Update()
    {
        Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
        transform.position += movement * Time.deltaTime * movement;
    }

    void jump()
    {
        if (Input.GetButtonDown("Jump"))
        {
            gameObject.GetComponent<RigidBody2D>().addForce(new Vector2(0f, 5f), forceMode2D.Impulse);
        };
    }
}

And there is couple issues with it:

  • Your float movement not inside the class, but above and therefore it throws you an error about that - your variables should be inside your class;
  • Your instantiated variable shouldn’t be named same as your class;
  • Pay attention to characters case, because RigidBody2D will throw an error, but Rigidbody2D will not, because programming language is case sensitive;
  • You are using a lot of libraries (using, using, …) that you are not implementing, but I won’t remove them, because maybe you will need them later, just keep in mind that grayed out using field means you are not implementing library that called.

The code below won’t throw any errors.

using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Security.Cryptography;
using System.Threading;
using UnityEngine;

public class movement : MonoBehaviour
{
    public float f_movement = 5f;

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

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

Hope that helps.