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

This is a part of a script and i got the report (A namespace cannot directly contain members such as fields or methods Line 1 and at “private Vector2 FindVel…”

i dont see the problem… if someone knows it help outta here pleaase :slight_smile:

private void CounterMovement(float x , float y, Vector2 mag) {
if (!grounded) return;
float threshold = 0.3f;
float multiplier = 0.3f;
print(mag.x);

if (x == 0) {
rb.AddForce(moveSpeed * transform.right * Time.deltaTime * -mag.x * multiplier);
}
if (y == 0) {
rb.AddForce(moveSpeed * transform.forward * Time.deltaTime * -mag.y * multiplier);
}
}

private Vector2 FindVelRelativeToLook() {
float lookAngle = transform.eulerAngles.y;
float moveAngle = Mathf.Atan2(y: rb.velocity.x, x: rb.velocity.z) * Mathf.Rad2Deg;

float u = Mathf.DeltaAngle(current: lookAngle, target: moveAngle);
float v = 90 - u;

float magnitue = rb.velocity.magnitue;
float yMag = magnitue * Mathf.Cos(f: u * Mathf.Deg2Rad);
float xMag = magnitue * Mathf.Cos(f: u * Mathf.Deg2Rad);

return new Vector2(xMag, yMag);

}

This kind of error usually indicates you have a problem with mismatched curly brackets { }

The error is saying one of your methods lives outside of the class definition. Your file should be structured like this:

// Using statements here
using UnityEngine;

// Class declaration starts here
public class MyClass : MonoBehaviour {

  // methods and member variables here
  int myVariable

  // beginning of a method here
  void MyMethod1() {

  // end of method here
  }

// End of class declaration here
}

if you have any methods or variables AFTER the closing bracket for the class, or BEFORE the class declaration, you will see that error. Check your code for mismatched braces and make sure everything is in the right place. Don’t look just at the line of code where the error is indicated. You also need to look at the context around it and make sure all scopes are properly opened and closed with brackets { }

Proper formatting and indenting of your code will make this kind of error very simple to track down.

2 Likes