"A Namespace Cannot Directly Contain Members Such as Fields or Methods"

A namespace cannot directly contain members such as fields or methods

The snippet you're showing doesn't seem to be directly responsible for the error.

This is how you can CAUSE the error:

namespace MyNameSpace
{
int i; <-- THIS NEEDS TO BE INSIDE THE CLASS

class MyClass
{
...
}
}

If you don't immediately see what is "outside" the class, this may be due to misplaced or extra closing bracket(s) }.

What does error A namespace cannot directly contain members such as fields or methods mean?

I would imagine you've declared that function outside of a class?

Like

namespace Something
{
private void Method()
{
}
}

Instead of

namespace Something
{
class MyClass
{
private void Method()
{
}
}
}

Error CS0116: A namespace cannot directly contain members such as fields or methods

To have instance members and methods, you need a class. You have confused a namespace with a class

namespace MyAwesomeNameSpace
{
public class ProgramRunningHelper
{
// put your class code here

}
}

Compiler Error CS0116

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

A namespace can contain other namespaces, structs, and classes.

C# errror CS0116: A namespace cannot directly contain members such as fields or methods

GameObject and that int skin need to be under Skins class, like Choise

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


public class Skins : MonoBehaviour {


public GameObject choise1;
public GameObject choise2;
public GameObject choise3;

public int skin = 1;
void Choise () {
if (Input.GetMouseDown(choise1)){
choise2.SetActive (false);
}
}
}

If you still have some problems here, because I don't know what are u trying to do with that choiseX, maybe you need to create them.
I mean, maybe u are trying to write something like this

void Choise () {
var GameObject choise1 = new GameObject();
var GameObject choise2 = new GameObject();
var GameObject choise3 = new GameObject();

int skin = 1;
if (Input.GetMouseDown(choise1)){
choise2.SetActive (false);
}
}

error CS0116: A namespace cannot directly contain members such as fields or methods in Unity

Remove the stray bracket;

public class SecondaryMenuControl : MonoBehaviour
{

public bool isSurvival;
public bool isSelectLevel;
public bool isGoBack;


} // remove this one!

// rest of the file

The error basically saids 'You defined some properties/fields/methods in a namespace' which you of course can't do. They all belong inside a class.

edit: this should be your output:

public class SecondaryMenuControl : MonoBehaviour
{

public bool isSurvival;
public bool isSelectLevel;
public bool isGoBack;

private void OnMouseUp(){
if(isSurvival)
{
SceneManager.LoadScene(MainScene);
}

if (isSurvival)
{
SceneManager.LoadScene(SelectLevelMenu);
}

if (isGoBack)
{
SceneManager.LoadScene(IntroMenu);
}

} // end of OnMouseUp
} // end of class

make sure you match an opening bracket with a closing one.

c# a namespace cannot directly contain members such as fields or methods

An identifier cannot start with a number; 3DBool in this case is invalid.

Reference, older but still correct: it must start with either a letter or the underscore character (including or excluding the @ character to disambiguate it with keywords).



Related Topics



Leave a reply



Submit