Search Unity

Space Shooter minor code problem

Discussion in 'Getting Started' started by Wolfgabe, Sep 6, 2016.

  1. Wolfgabe

    Wolfgabe

    Joined:
    Sep 4, 2016
    Posts:
    131
    Recently I just set the boundries but when I go to test I get this error at the very start that says a namespace cannot directly contain members such as fields or methods. Any soluition to this problem. I have seen people mention about changing the class but I am not sure. Part of me is guessing that in Unity 5 you dont need to serialize the boundries since they already showed up before I entered the serializable code

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [System.Serializable}
    5. public class Boundary
    6. {
    7.     public float xMin, xMax, zMin, zMax;
    8. }
    9.  
    10. public class PlayerController : MonoBehaviour
    11. {
    12.     public float speed;
    13.     public Boundary boundary;
    14.  
    15.     void FixedUpdate ()
    16.     {
    17.         float moveHorizontal = Input.GetAxis ("Horizontal");
    18.         float moveVertical = Input.GetAxis ("Vertical");
    19.  
    20.         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    21.         GetComponent<Rigidbody>().velocity = movement * speed;
    22.  
    23.         GetComponent<Rigidbody> ().position = new Vector3
    24.         (
    25.             Mathf.Clamp (GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax),
    26.             0.0f,
    27.             Mathf.Clamp (GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax)
    28.         );
    29.     }
    30. }
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I think the problem is just the } instead of a ] on line 4.

    (And when posting about an error message, please always copy/paste the exact error.)
     
  3. Wolfgabe

    Wolfgabe

    Joined:
    Sep 4, 2016
    Posts:
    131
    I fixed that and still didnt work

    it occurs right at the start in the using UnityEngine part on line 1
     
  4. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Son of a... I looked through the pasted code 3 times, knowing it was something minor and yet still couldn't see it! It's this lack of attention to detail that, in the event of alien invaders replacing humans with replicants, makes me sure I'd never notice a thing and be at extreme risk of assimilation myself.

    The error code likely changed, though. Please paste the actual error here as Joe said.
     
    JoeStrout likes this.
  5. Wolfgabe

    Wolfgabe

    Joined:
    Sep 4, 2016
    Posts:
    131
    A namespace cannot directly contain members such as fields or methods (CS0116) does this help?
     
  6. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Not really.

    Is this all of the code in that file? Is the file named the same thing as your class inheriting from MonoBehavior (PlayerController)? Do you have any other scripts called PlayerController that might be the actual problem?

    That error message is saying you have something outside of a class definition that you're not supposed to. Since it doesn't seem like there's anything here that shouldn't be there, there's gotta be something going on we're not seeing.
     
  7. Wolfgabe

    Wolfgabe

    Joined:
    Sep 4, 2016
    Posts:
    131
    Yes this is all the code in the file. Whats wierd is that when I look at the player controller script in the main editor it doesnt show the code I wrote all it shows is just a u

    EDIT: Okay what I did was I deleted my script then I repasted everything I typed previously. Now it seems to compile just fine
     
  8. Wolfgabe

    Wolfgabe

    Joined:
    Sep 4, 2016
    Posts:
    131
    Okay now I have a new problem I did everything right but in the editor I see this error message that says namespace global already contains a definition for boundary
     
  9. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Then you have 'boundary' defined in more than one file.

    And you're still not copying/pasting the full error — we can tell because the full error text includes the file name and line number (which is useful information). Just click on the error in the Console, hit Copy, switch to the forum, and Paste. It seems to me you actually have to work harder (type more) to give us a less useful description of the problem!
     
  10. Wolfgabe

    Wolfgabe

    Joined:
    Sep 4, 2016
    Posts:
    131
    Okay here is the full error I think Assets/Scripts/PlayerController.cs(5,14): error CS0101: The namespace `global::' already contains a definition for `Boundary' I did a bit of searching and I found another script in the pre fabs folder that may have something to do with it

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Done_DestroyByBoundary : MonoBehaviour
    5. {
    6.     void OnTriggerExit (Collider other)
    7.     {
    8.         Destroy(other.gameObject);
    9.     }
    10. }

    sorry if I wasnt being very descriptive enough
     
  11. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    In case you're not sure what you're doing exactly, here's a bit o' advice. You're actually defining two classes inside your script file. While this is perfectly legal, I find it tends to lead to problems exactly like this. It's not a horrible idea to keep your classes all in separate files. It doesn't create any performance problems, and what small time cost is associated with switching between files is more than offset by the saved time in debugging (as you're discovering now!).

    Additionally, your class is pretty basic, and depending on how you're using it, may be better replaced with a struct. But that's not the source of your problems, so you can forget that for now.

    Boundary is kind of a horrible name for a class because of how undescriptive it is. Boundary for what? If you intend for this to be a be-all use case for boundaries, then okay, but chances are you only want to use this for the boundary of your player controller. Consider naming it to match (PlayerControllerBoundary).

    The segment you just posted above isn't the culprit, as that class is called Done_DestroyByBoundary. That's different than just Boundary. Somewhere, though, you have a class defined for Boundary that's causing a conflict here. It could be a plugin or a Standard Asset or a class you defined inside another class's script file and forgot about (which is why we don't want to do that!)
     
  12. Wolfgabe

    Wolfgabe

    Joined:
    Sep 4, 2016
    Posts:
    131
    When I imported the assets there was also a folder that has the completed scripts and assets I think that may be what I causing the conflict
     
  13. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Absofruitly! I'd remove the completed scripts from the project entirely. Keep 'em in your Downloads folder or something, and only open them to check your work and such.
     
  14. Wolfgabe

    Wolfgabe

    Joined:
    Sep 4, 2016
    Posts:
    131
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [System.Serializable]
    5. public class Done_Boundary
    6. {
    7.     public float xMin, xMax, zMin, zMax;
    8. }
    9.     public class Done_PlayerController : MonoBehaviour
    10.     {
    11.    
    12.         public float speed;
    13.         public float tilt;
    14.         public Done_Boundary boundary;
    15.  
    16.         void FixedUpdate ()
    17.         {
    18.             float moveHorizontal = Input.GetAxis ("Horizontal");
    19.             float moveVertical = Input.GetAxis ("Vertical");
    20.  
    21.             Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    22.             GetComponent<Rigidbody>().velocity = movement * speed;
    23.  
    24.             GetComponent<Rigidbody> ().position = new Vector3
    25.                 (
    26.                     Mathf.Clamp (GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax),
    27.                     0.0f,
    28.                     Mathf.Clamp (GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax)
    29.                 );
    30.         GetComponent<Rigidbody> ().rotation = Quaternion.Euler (0.0f, 0.0f, Rigidbody.velocity.x * -tilt);
    31.         }
    32.     }
    33.  
    34.  

    This is the code I have written so far now I keep getting error CS0120 Assets/Scripts/PlayerController.cs(30,95): error CS0120: An object reference is required to access non-static member `UnityEngine.Rigidbody.velocity'


    Also when I went to test the whole background moves with the ship I tried getting rid of the done assets but I think I screwed something up
     
  15. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Awesome! That (30,95) after the file name is the exact location of the error. The first is the line number, and the second is the column (character) within that line. Occasionally the line number may be off by one or so, but always start by looking at the exact line specified, which in the code you provided is:

    Code (CSharp):
    1.         GetComponent<Rigidbody> ().rotation = Quaternion.Euler (0.0f, 0.0f, Rigidbody.velocity.x * -tilt);
    OK, so could this be causing the error? It's complaining about `UnityEngine.Rigidbody.velocity'. Because you have "using UnityEngine;" at the top of your script, your code doesn't have to actually say UnityEngine all over the place; it could be just "Rigidbody.velocity". Does this line of code say that? ...Yep! Right there (probably around column 95, though I rarely bother to actually count and see).

    Now what's its complaint about it? "An object reference is required to access non-static member". So it's saying that "velocity" is a plain old member of the Rigidbody class; every Rigidbody instance has one (as opposed to a static member, where there is only one value shared by all instances of the class). So you can't just reference the velocity of Rigidbody in general; you need to reference the velocity of some particular rigidbody. Your code says Rigidbody.velocity, so it's not doing that; it's trying to access velocity via just the class name (Rigidbody).

    At this point, as a beginner, you should probably just compare your code to the code in the tutorial, very carefully, keeping in mind that case matters (Rigidbody is not the same as rigidbody).

    But since I've gone this far, I'll go ahead and tell you: this probably wanted to be "rigidbody.velocity" with a lowercase "r". Lowercase "rigidbody" is not the name of a class. It is, instead, a variable accessible from any MonoBehaviour script, which years ago, was a convenient shortcut for GetComponent<Rigidbody>(). Nowadays it's just a convenient way to get the compiler to bark at you and tell you that this shortcut is no longer available.

    So, you should replace Rigidbody there with GetComponent<RIgidbody>(), and you'll be all set. Of course now this code really has GetComponent<RIgidbody>() all over the place, which makes the code hard to read and also slower than it needs to be. We could discuss how to fix that if you want... or you can just get back on with the tutorial!
     
  16. Wolfgabe

    Wolfgabe

    Joined:
    Sep 4, 2016
    Posts:
    131

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [System.Serializable]
    5. public class Done_Boundary
    6. {
    7.     public float xMin, xMax, zMin, zMax;
    8. }
    9.     public class Done_PlayerController : MonoBehaviour
    10.     {
    11.  
    12.         public float speed;
    13.         public float tilt;
    14.         public Done_Boundary boundary;
    15.  
    16.         void FixedUpdate ()
    17.         {
    18.             float moveHorizontal = Input.GetAxis ("Horizontal");
    19.             float moveVertical = Input.GetAxis ("Vertical");
    20.  
    21.             Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    22.             GetComponent<Rigidbody>().velocity = movement * speed;
    23.  
    24.             GetComponent<Rigidbody>().position = new Vector3
    25.                 (
    26.                     Mathf.Clamp (GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax),
    27.                     0.0f,
    28.                     Mathf.Clamp (GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax)
    29.                 );
    30.         GetComponent<Rigidbody>().rotation = Quaternion.Euler (0.0f, 0.0f, GetComponent<Rigidbody>().velocity.x * -tilt);
    31.         }
    32.     }
    33.  
    34.  
    okay here is the new code. but for some reason it says in the editor the associated script cannot be loaded. It says in the console the referenced script on this Behavior (Game Object Player) is missing!
     
  17. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    That error occurs when the name of the class in the file (here Done_Boundary) doesn't match the actual file name (which would have to be Done_Boundary.cs in this case).
     
  18. Wolfgabe

    Wolfgabe

    Joined:
    Sep 4, 2016
    Posts:
    131
    So I am guessing I change the name of the file?


    EDIT: Okay now I have a new problem I changed the name of the script file now it says The class named 'Done_Boundary' is not derived from MonoBehaviour or ScriptableObject! Also even when I have redone the script it now keeps saying the associated script cannot be loaded
     
    Last edited: Sep 7, 2016
  19. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Oops. My mistake: I looked at just the first class you had defined, and failed to notice that it was not the MonoBehaviour class. The MonoBehaviour here is Done_PlayerController, so the file must be called Done_PlayerController.cs.

    Like @Schneider21, I don't generally recommend you have multiple classes in one file. Done_Boundary should either be moved into its own Done_Boundary.cs file, or you should move it inside the Done_PlayerController class. Just put it right up there with "public float speed" and friends. That not only clarifies what class you're defining in this file, but also avoids polluting the global namespace with another class that really only relates to Done_PlayerController.
     
  20. Wolfgabe

    Wolfgabe

    Joined:
    Sep 4, 2016
    Posts:
    131

    I tried it and the code is finally working correctly thank you so much. Its really annoying how these tutorials are dated. I was often confused on alot of things
     
  21. angelalvizo8

    angelalvizo8

    Joined:
    Apr 9, 2018
    Posts:
    2
    some of the codes like "input","mathf", "vector 3","rigidbody" or the replacement for the rigidbody component .what is wrong with the coding
     
  22. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
  23. Bill_Martini

    Bill_Martini

    Joined:
    Apr 19, 2016
    Posts:
    445
    Yeah, me too!?!

    I think he's whining about the deprecated use of rigidbody that's found in old scripts laying around.

    @angelalvizo8 start here. Learn to debug early.
     
    JoeStrout likes this.
  24. angelalvizo8

    angelalvizo8

    Joined:
    Apr 9, 2018
    Posts:
    2
    a couple days too late to see the responds and i already figured them out . also note it would be a great idea for unity to make updated videos for any coding updates for the old tutorial videos or any coding updates in general so it doesn't have to be easy miss texts in the old tutorial videos about the code updates and giving better explanation about the updated texts