Search Unity

Namespace cannot directly contain members such as fields or methods (DayNight script)

Discussion in 'Getting Started' started by phantomunboxing, Jan 4, 2016.

  1. phantomunboxing

    phantomunboxing

    Joined:
    Nov 10, 2015
    Posts:
    43
    I just started coding (So sorry if I sound like a noob) and made a day and night script for Unity 3D 5. Yesterday it was working fine until I accidently deleted it. I copied the script and now I am geting the error "A namespace cannot directly contain members such as fields or methods). I am new to this website

    var cyclemins : float;
    var cyclecalc : float;

    cyclemins = 12;
    cyclecalc = 0.1/cyclemins *-1;

    function Update () {
    transform.Rotate(0, 0, cyclecalc, Space.World);
    }

    Here is a link to the file on mediafirehttp://www.mediafire.com/download/bbeu88jido8x82n/DayNight.cs

    For my setup: Dell XPS 8500 Windows 10 Monodevelop (Tried visual studio 2015 but monodevelop was better, transfered file) Dual Monitors

    Unity: Unity 5.3.1f1 64 bit
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Your script is incomplete. That Update() function and those properties need to be inside a MonoBehaviour subclass. I don't use UnityScript, so I can't type the exact syntax out of my head (and btw, since you're just starting anyway, I highly recommend you switch to C#). But if you create a new script, it should give you the basic structure of a MonoBehaviour, and then you can fill in your properties and Update code.
     
    pat600130, COder17 and Ryiah like this.
  3. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Are you writing UnityScript into a script that ends in .cs, so the compiler thinks it's a C# script?
     
    Kiwasi, Ryiah and jhocking like this.
  4. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,175
    That is not a valid C# script. You have to put this inside a class definition. You're using an Update method so this obviously is intended to be a MonoBehavior attached to an object. Try this.

    By the way the class name has to be the same as the C# filename. For this script it would be DayNight.

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public class DayNight: MonoBehavior {
    4.     public float cyclemins;
    5.     public float cyclecalc;
    6.  
    7.     function Start() {
    8.         cyclemins = 12;
    9.         cyclecalc = 0.1/cyclemins *-1;
    10.     }
    11.  
    12.     function Update () {
    13.         transform.Rotate(0, 0, cyclecalc, Space.World);
    14.     }
    15. }
     
    Last edited: Jan 5, 2016
    MTDues likes this.
  5. phantomunboxing

    phantomunboxing

    Joined:
    Nov 10, 2015
    Posts:
    43
    Yup you are correct sir... I solved the problem but did not update thread sorry
     
  6. MTDues

    MTDues

    Joined:
    Mar 27, 2014
    Posts:
    27
    Hwo to fix this error too it says the same proplem and im new to coding

    Code (CSharp):
    1.      var target : Transform; //the enemy's target
    2.      var moveSpeed = 3; //move speed
    3.      var rotationSpeed = 3; //speed of turning
    4.    
    5.      var myTransform : Transform; //current transform data of this enemy
    6.  
    7. RaycastHit myHit;
    8. var offsetAlign:float = 0.0; //Add offset to make your object align perfect to terrain
    9. if(Physics.Raycast (myTransform.position, Vector3.down, myHit))
    10. {
    11.     myTransform.position.y = myHit.point + offsetAlign;
    12. }
    13.      {
    14.           target = GameObject.FindWithTag("Player").transform; //target the player
    15.    
    16.      }
    17.    
    18.      function Update () {
    19.          //rotate to look at the player
    20.          myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
    21.          Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
    22.    
    23.          //move towards the player
    24.          myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
    25.    
    26.    
    27.      }

    Says at the line 1 error name space cannot directly conatin
     
  7. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    As in the other posts in this thread: you can't have code just hanging out like that (lines 7-16). All code, except for property declarations, needs to be inside some function.

    I suggest (1) abandoning UnityScript and switching instead to C#, and then (2) doing some tutorials, like these. You won't get very far at all just copying & pasting random bits of code without understanding what you're doing. You need a grounding in how programming works, and those tutorials should give you that.
     
    Ryiah likes this.
  8. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    For the record, you don't actually need to explicitly specify classes in UnityScript. It's one of the syntactic sugars the language is known for. If you don't declare a class, Unity will infer that it should inherit from MonoBehaviour for you.

    The most likely problem is trying to write UnityScript in a .cs file. Or trying to do UnityScript at all with visual studio.

    As always, the best solution is to switch to C#. However if you fix the above problems, you can use UnityScript. Just don't expect much help from the forums, at last count we were down to one expert user with a full grasp of UnityScript who regularly hangs out in the scripting sections.
     
    Ryiah likes this.
  9. galstyanguga

    galstyanguga

    Joined:
    Jun 10, 2020
    Posts:
    1
    Hi I have a problem im getting the same error my code is
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class BoxMover : MonoBehaviour {
    public float SpinSpeed = 180f;

    }



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

    Vector3 SpinSpeed = default;
    GameObject.transform.Rotate(Vector3.up * SpinSpeed * Time.deltaTime);

    }
    can you pls help me
     
    pmakepeace likes this.
  10. AaradhyaPurao

    AaradhyaPurao

    Joined:
    Jan 23, 2021
    Posts:
    1
    I am also new to unity, I am also facing the same error, my code is, :-
    Code (CSharp):
    1. using System;
    2. using System.Numerics;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6. using Vector3 = UnityEngine.Vector3;
    7. int _variable = UnityEngine.Random.Range(1, 20);
    8.  
    9. public class PlayerController : MonoBehaviour {
    10.  
    11.     [HideInInspector]
    12.     public PlayerDirection direction;
    13.  
    14.     [HideInInspector]
    15.     public float step_Length = 0.2f;
    16.  
    17.     [HideInInspector]
    18.     public float movement_Frequency = 0.1f;
    19.  
    20.     private float counter;
    21.     private bool move;
    22.  
    23.     [SerializeField]
    24.     private GameObject tailPrefab;
    25.  
    26.     private List<Vector3> delta_Position;
    27.  
    28.     private List<Rigidbody> nodes;
    29.  
    30.     private Rigidbody main_Body;
    31.     private Rigidbody head_Body;
    32.     private Transform tr;
    33.  
    34.     private bool create_Node_At_Tail;
    35.  
    36.     void Awake() {
    37.      
    38.         tr = transform;
    39.         main_Body = GetComponent<Rigidbody>();
    40.  
    41.         InitSnakeNodes();
    42.         InitPlayer();
    43.  
    44.  
    45.     }
    46.  
    47.     // Update is called once per frame
    48.     void Update() {
    49.      
    50.     }
    51.  
    52.     void InitSnakeNodes() {
    53.      
    54.         nodes = new List<Rigidbody>();
    55.  
    56.         nodes.Add(tr.GetChild(0).GetComponent<Rigidbody>());
    57.         nodes.Add(tr.GetChild(1).GetComponent<Rigidbody>());
    58.         nodes.Add(tr.GetChild(2).GetComponent<Rigidbody>());
    59.  
    60.         head_Body = nodes[0];
    61.  
    62.     }
    63.  
    64.     void SetDirectionRandom() {
    65.  
    66.         int dirRandom = Random.Range(0, (int)PlayerDirection.COUNT);
    67.         direction = (PlayerDirection)dirRandom;
    68.  
    69.     }
    70.  
    71.     void InitPlayer() {
    72.  
    73.         SetDirectionRandom();
    74.  
    75.         switch (direction) {
    76.             case PlayerDirection.RIGHT:
    77.  
    78.                 nodes[1].position = nodes[0].position - new Vector3(Metrics.NODE, 0f, 0f);
    79.                 nodes[2].position = nodes[0].position - new Vector3(Metrics.NODE * 2f, 0f, 0f);
    80.  
    81.                 break;
    82.             case PlayerDirection.LEFT:
    83.  
    84.                 nodes[1].position = nodes[0].position + new Vector3(Metrics.NODE, 0f, 0f);
    85.                 nodes[2].position = nodes[0].position + new Vector3(Metrics.NODE * 2f, 0f, 0f);
    86.  
    87.                 break;
    88.             case PlayerDirection.UP:
    89.  
    90.                 nodes[1].position = nodes[0].position - new Vector3(0f, Metrics.NODE, 0f);
    91.                 nodes[2].position = nodes[0].position - new Vector3(0f, Metrics.NODE * 2f, 0f);
    92.  
    93.                 break;
    94.             case PlayerDirection.DOWN:
    95.  
    96.                 nodes[1].position = nodes[0].position + new Vector3(0f, Metrics.NODE, 0f);
    97.                 nodes[2].position = nodes[0].position + new Vector3(0f, Metrics.NODE * 2f, 0f);
    98.  
    99.                 break;
    100.         }
    101.      
    102.  
    103.     }
    104.  
    105. }
    106.  
    sorry for a such a long code,
    I watched a video for this code (I watched the half video only ) :-
    FROM:- [
    ]
    till "20:46",
    the owner of the video didn't got this error
    please help me with it
     
    Last edited: Jan 23, 2021
  11. kalwandvok

    kalwandvok

    Joined:
    Jan 22, 2021
    Posts:
    1
    damn , 5 years later , saves my ass
     
  12. Deleted User

    Deleted User

    Guest

    I am facing an error in the c# script The Error is CS0116 and I am a beginer
    please Help Me ASAP



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class NewBehaviourScript : MonoBehaviour{
    7.  
    8.     public float TheDistance;
    9.     public GameObject ActionDsiplay;
    10.     public GameObject ActionText;
    11.     public GameObject TheDoor;
    12.     public AudioSource CreakSound;
    13.  
    14.     void Update() {
    15.         TheDistance = PlayerCasting.DistanceFromTarget;
    16.     }
    17. }
    18.  
    19. void OnMouseOver() {
    20.     if (TheDistance <= 3) {
    21.         ActionDsiplay.SetActive (true);
    22.         ActionText.SetText (true);
    23.     }
    24.     if (Input.GetButtonDoun("Action")) {
    25.         if (TheDistance <= 3) {
    26.         this.GameComponent<BoxCollider>().enabled = false;
    27.         ActionDsiplay.SetActive(false);
    28.         ActionText.SetActive(false);
    29.         TheDoor.GameComponent<Animation> ().Play ("DOORHINGE Animation");
    30.         CreakSound.Play () ;
    31.  
    32.          }
    33.     }
    34. }
    35.  
    36.     void OnMouseExit() {
    37.         ActionDsiplay.SetActive (false);
    38.         ActionText.SetActive (false);
    39. }
    40.    
     
    Last edited by a moderator: May 4, 2021
  13. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    OK. You did one thing right: you properly quoted your code in a CODE block. Lots of newbies screw that up, so good job there.

    However, you also did several things wrong. When you have a question, please follow these rules:
    1. Start a new thread. Don't reply to an old thread unless you are sure that your issue is the same as the one the old thread is about.
    2. Include the text of the error, not just the number (we don't all sit around using flashcards to memorize C# error numbers).
    3. Be sure that text includes the file name and line number, and that the line mentioned is among the code you are showing.
    4. Describe what you have already done to attempt to fix it, and why that didn't work.
    Good luck!
    - Joe
     
  14. bebcantcode

    bebcantcode

    Joined:
    Aug 1, 2021
    Posts:
    1
    same happening here, instead with this code, can anyone help me?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnemyDamage : MonoBehaviour
    6. {
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.        
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update()
    15.     {
    16.        
    17.     }
    18. }
    19. private void OnTriggerEnter(Collider other)
    20. {
    21.     if (other.tag == "Melee")
    22.         Destroy(gameObject);//Or apply a damage method
    23. }
     
  15. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    The
    OnTriggerEnter
    method is outside of your class. It needs to be within the opening/closing curly-braces of the class.
     
  16. Schmordan

    Schmordan

    Joined:
    Aug 1, 2021
    Posts:
    1
    plz help the unity engine thinks the ' public Rigidbody2D theRB;' thing is invalid. the error states the position of the error is 6, 20, which is the rigidbody2d name??? IDK. code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5.  
    6. public RigidBody2D theRB;
    7. private float inputX;
    8. public float moveSpeed, jumpForce;
    9.  
    10. public class InputScript : MonoBehaviour
    11. {
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.      
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.         theRB.velocity = new Vector2(inputX * moveSpeed, theRB.velocity.y);
    22.     }
    23.     public void Move(InputAction.CallbackContext ctx)
    24.     {
    25.         inputx = ctx.ReadValue<Vector2>().x;
    26.     }
    27. }
    28.  
     
  17. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Your fields need to be inside the class.
     
    rajanibadri97 and Joe-Censored like this.