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
How to Parse (Big) Xml in C# Code
Wpf Webbrowser Control - How to Suppress Script Errors
Writing Large Number of Records (Bulk Insert) to Access in .Net/C#
New Keyword in Method Signature
How to Read and Modify Ntfs Alternate Data Streams Using .Net
How to Decode a Url Parameter Using C#
Optimal Linq Query to Get a Random Sub Collection - Shuffle
Changing the User Agent of the Webbrowser Control
Why Can't I Have Abstract Static Methods in C#
System.Text.JSON.JSONelement Toobject Workaround
Best Way to Repeat a Character in C#
Where Does System.Diagnostics.Debug.Write Output Appear
How to Call the 'Base Implementation' of an Overridden Virtual Method