Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

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:
    8,283
    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.
     
    COder17 and Ryiah like this.
  3. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,670
    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:
    14,755
    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:
    8,283
    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,533
    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.