- Home /
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<RigidBody2D>().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
Answer by ADiSiN · May 08, 2020 at 09:47 PM
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.
Almost everything is right, except for jump script section. Jump is not working. Fix it pls beacuse I dont know how to write scripts.
Jump doesn't work because jump() never gets called. Refer to this for more info: https://docs.unity3d.com/ScriptReference/Rigidbody2D.AddForce.html
Your answer
![](https://koobas.hobune.stream/wayback/20220612233637im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity Teams Errors When Transferring 0 Answers
How to make the tower shoot at the one who is closer to the finish line? 0 Answers
[Help!]Unity FPS Health script error. 1 Answer
The type or namespace "UnityEngine" could not be found - Can not edit scripts 2 Answers
Unexpected symbol '(' in class, or interface member declaration 1 Answer