Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

A namespace cannot directly contain members such as fields or methods

Discussion in 'Scripting' started by anmolks, Nov 26, 2019.

  1. 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;
    }
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    Use [code ]code tags[/code] when posting code (which will give us formatting, tabs, and line numbers) and copy and paste the entire error message (which will, among other things, point to the exact line of code where the problem is).
     
  3. anmolks

    anmolks

    Joined:
    Aug 31, 2014
    Posts:
    4
    upload_2019-11-26_23-19-15.png
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class NewBehaviourScript : MonoBehaviour
    6. {  
    7. public float distance;
    8. public int count;
    9. public string message;
    10. private bool finished;
    11.    
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         Debug.Log("Bird Game");
    16.    
    17.  
    18.    
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.         if (Input.GetKey("right"))
    25.             //Flip("right");
    26.             {transform.Translate (.04f,0f,0f);
    27.         }
    28. if (Input.GetKey("left"))
    29.         //Flip("left");
    30.             {transform.Translate (-.04f,0f,0f);
    31.         }
    32. if (Input.GetKey("up"))
    33.  
    34.             {transform.Translate (0f,.04f,0f);
    35.         }
    36.         if (Input.GetKey("down"))
    37.  
    38.             {transform.Translate (0f,-.04f,0f);
    39.         }}
    40. }
    41. public void Flip( string direction){
    42.     var thescale = transform.localScale;
    43.    
    44.     if(direction == "right") {
    45.         thescale.x = 1;      
    46.     } else {
    47.         thescale.x = -1;
    48.     }
    49.     transform.localScale =thescale;
    50.     }
    Thanks man!

    upload_2019-11-26_23-19-15.png upload_2019-11-26_23-19-15.png
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    The error points to line 53 and you've posted a 50-line script... something's missing. Where does it go when you double-click the error?

    I can say for sure that your { curly brackets } are a mess. My first suggestion would be that you should try putting every curly bracket on its own line, and see if they match up correctly. Generally, the error means that you have something outside of the class block. So like:
    Code (csharp):
    1. public class NewBehaviourScript : MonoBehaviour
    2. {
    3. blah blah blah
    4. } //this matches with the { right after the "class" line
    5.  
    6. public void Flip(string direction) { // This is bad, it needs to be INSIDE the above  { }'s
    7. }
     
  5. anmolks

    anmolks

    Joined:
    Aug 31, 2014
    Posts:
    4
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class NewBehaviourScript : MonoBehaviour
    6. {  
    7. public float distance;
    8. public int count;
    9. public string message;
    10. private bool finished;
    11.    
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         Debug.Log("Bird Game");
    16.    
    17.  
    18.    
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {if (Input.GetKey("right"))
    24.             //Flip("right");
    25.             {transform.Translate (.04f,0f,0f);}
    26. if (Input.GetKey("left"))
    27.         //Flip("left");
    28.             {transform.Translate (-.04f,0f,0f);}
    29. if (Input.GetKey("up"))
    30. {transform.Translate (0f,.04f,0f);}
    31.        
    32.         if (Input.GetKey("down"))
    33.            
    34. {transform.Translate (0f,-.04f,0f);}
    35. } }
    36. public void Flip( string direction){
    37.     var thescale = transform.localScale;
    38.    
    39.     if(direction == "right") {
    40.         thescale.x = 1;      
    41.     } else {
    42.         thescale.x = -1;
    43.     }
    44.     transform.localScale =thescale;
    45.     }
    46.  
    47.  
    48.  
    49.  

    Sorry for being so messy. Some how I have edited the code after taking the screen shot , actually this is my first day of learning script.
    attaching new screen shot.
    upload_2019-11-26_23-41-39.png
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    BTW, you can just ctrl-C and directly copy the error out of the console, no need for screenshots.

    My advice is still the same, put every { and } on a line by itself, that way you'll be able to match them up and see what is outside the main class's brackets, and move it inside.
     
    anmolks likes this.
  7. Rok2591

    Rok2591

    Joined:
    Aug 16, 2017
    Posts:
    2
    Line 35 has an extra bracket.
     
  8. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    As StarManta pointed out, your formatting is a mess. That's going to create bugs. If you don't follow a particular format and be consistent about it, you make your own code harder to read. If it's harder to read, then it's harder to spot what's wrong with it.

    Fix your formatting first, then review your code to see what's wrong. What IDE are you using? If you're using Visual Studio Community or Visual Studio Code, it should've helped you auto format your code.

    In this particular case, here are where I can see some stray brackets. The first bracket is actually fine but very confusing to read because it looks like it's part of the if statement, and not as an opening bracket for Update():

    upload_2019-11-26_10-57-22.png