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

I am brand new to coding and don’t know why I’m getting this error, this is my code to anyone that can help!

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

private int selectedZombiePosition = 0;
public class GameManager: MonoBehaviour {
public GameObject selectedZombie;
public List zombies;
public Vector3 selectedsize;
public Vector3 defaultsize;

// Use this for initialization
void Start () {
SelectZombie (selectedZombie);
}

// Update is called once per frame
void Update () {

if (Input.GetKeyDown (“left”)) {
GetZombieLeft ();
}

if (Input.GetKeyDown (“right”)) {
GetZombieRight ();
}

if (Input.GetKeyDown (“up”)) {

}
}

void GetZombieLeft() {
if (selectedZombiePosition == 0) {
SelectZombie (zombies [3]);
} else {
GameObject newZombie = zombies [selectedZombiePosition - 1];
SelectZombie (newZombie);
}
}

void GetZombieRight() {
if (selectedZombiePosition == 3) {
SelectZombie (zombies[0]);
} else {
SelectZombie (zombies [selectedZombiePosition + 1]);
}
}
void SelectZombie(GameObject newZombie) {
newZombie.transform.localScale = selectedsize;
}
}

Please use formatting to make your code readable. See the first post in the forum.

In other news, everything in a source file besides using and namespace pretty much has to be within the class definition brackets. You have at least one line outside of the class.

Hello,
I am new to the C# I am facing same issue with the same error. can anyone point out whats wrong with my code.

It would be great help. thanks in advance.

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

public class NewBehaviourScript : MonoBehaviour
{

// Start is called before the first frame update
void Start()
{
Debug.Log(“Bird Game”);

}

// Update is called once per frame
void Update()
{
if (Input.GetKey(“right”))
Flip(“right”);
{transform.Translate (.04f,0f,0f);
}
if (Input.GetKey(“left”))
Flip(“left”);
{transform.Translate (-.04f,0f,0f);
}
if (Input.GetKey(“up”))

{transform.Translate (0f,.04f,0f);
}
if (Input.GetKey(“down”))

{transform.Translate (0f,-.04f,0f);
}}
}
public void Flip( string direction){
var thescale = transform.localScale;

if(direction == “right”) {
thescale.x = 1;
} else {
thescale.x = -1;
}
transform.localScale =thescale;
}

Did you read the first line of my 9/16 post above about using code formatting?

1 Like

https://discussions.unity.com/t/481379

For every “}” you need a “{” but you’re not doing so or you’re placing them in the wrong places.

Oh man, all these years, I’ve been doing it wrong! NOW you tell me… :slight_smile: