Space Shooter minor code problem

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

using UnityEngine;
using System.Collections;

[System.Serializable}
public class Boundary
{
    public float xMin, xMax, zMin, zMax;
}

public class PlayerController : MonoBehaviour
{
    public float speed;
    public Boundary boundary;

    void FixedUpdate ()
    {
        float moveHorizontal = Input.GetAxis ("Horizontal");
        float moveVertical = Input.GetAxis ("Vertical");

        Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
        GetComponent<Rigidbody>().velocity = movement * speed;

        GetComponent<Rigidbody> ().position = new Vector3
        (
            Mathf.Clamp (GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax),
            0.0f,
            Mathf.Clamp (GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax)
        );
    }
}

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.)

I fixed that and still didnt work

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

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.

1 Like

A namespace cannot directly contain members such as fields or methods (CS0116) does this help?

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.

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

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

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!

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

using UnityEngine;
using System.Collections;

public class Done_DestroyByBoundary : MonoBehaviour
{
    void OnTriggerExit (Collider other)
    {
        Destroy(other.gameObject);
    }
}

sorry if I wasnt being very descriptive enough

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!)

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

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.

using UnityEngine;
using System.Collections;

[System.Serializable]
public class Done_Boundary
{
    public float xMin, xMax, zMin, zMax;
}
    public class Done_PlayerController : MonoBehaviour
    {
   
        public float speed;
        public float tilt;
        public Done_Boundary boundary;

        void FixedUpdate ()
        {
            float moveHorizontal = Input.GetAxis ("Horizontal");
            float moveVertical = Input.GetAxis ("Vertical");

            Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
            GetComponent<Rigidbody>().velocity = movement * speed;

            GetComponent<Rigidbody> ().position = new Vector3
                (
                    Mathf.Clamp (GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax),
                    0.0f,
                    Mathf.Clamp (GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax)
                );
        GetComponent<Rigidbody> ().rotation = Quaternion.Euler (0.0f, 0.0f, Rigidbody.velocity.x * -tilt);
        }
    }

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

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:

        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(). 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(), and you’ll be all set. Of course now this code really has GetComponent() 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!

using UnityEngine;
using System.Collections;

[System.Serializable]
public class Done_Boundary
{
    public float xMin, xMax, zMin, zMax;
}
    public class Done_PlayerController : MonoBehaviour
    {
  
        public float speed;
        public float tilt;
        public Done_Boundary boundary;

        void FixedUpdate ()
        {
            float moveHorizontal = Input.GetAxis ("Horizontal");
            float moveVertical = Input.GetAxis ("Vertical");

            Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
            GetComponent<Rigidbody>().velocity = movement * speed;

            GetComponent<Rigidbody>().position = new Vector3
                (
                    Mathf.Clamp (GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax),
                    0.0f,
                    Mathf.Clamp (GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax)
                );
        GetComponent<Rigidbody>().rotation = Quaternion.Euler (0.0f, 0.0f, GetComponent<Rigidbody>().velocity.x * -tilt);
        }
    }

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!

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).

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

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.

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