'How do you fix this error: A namespace cannot directly contain members such as fields or methods [closed]

I am using unity code to make a game, and I get this error:

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

This is my code:

public class MainMenu : MonoBehaviour
{
    public void PlayGame ()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    }
}   

public void GQ ()
{
    Debug.Log ("QUIT!");
    Application.Quit();
}


Solution 1:[1]

Put your GQ() method code inside the class.

public class MainMenu : MonoBehaviour
{
    public void PlayGame ()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    }

    public void GQ ()
    {
        Debug.Log ("QUIT!");
        Application.Quit();
    }
}   

Your Unity C# script can contain only 1 public class. Every attributes and methods have to be defined only inside the class. Nothing except the imports should be written outside the class

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Geeky Quentin