Search Unity

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

Discussion in 'Scripting' started by JExBo, Sep 16, 2019.

  1. JExBo

    JExBo

    Joined:
    Sep 16, 2019
    Posts:
    1
    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<GameObject> 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;
    }
    }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    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.
     
  3. anmolks

    anmolks

    Joined:
    Aug 31, 2014
    Posts:
    4
    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;
    }
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Did you read the first line of my 9/16 post above about using code formatting?
     
    Joe-Censored likes this.
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    https://forum.unity.com/threads/using-code-tags-properly.143875/

    For every "}" you need a "{" but you're not doing so or you're placing them in the wrong places.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Oh man, all these years, I've been doing it wrong! NOW you tell me... :)