Chipsanddip 5
Chipsanddip 5

Reputation: 27

A namespace cannot directly contain members

I'm very new to unity and coding ( c# specifically ) and when I was working on the moving I kept getting this error . I've tried researching and looking at other questions but wasn't sure on what to do, so if you can help me that will be very helpful. the errors in unity here is the code

Upvotes: 1

Views: 173

Answers (3)

Adrian Efford
Adrian Efford

Reputation: 483

Remove the "}" after Monobehavoir and paste it at the end of your script. Your code should look like this:


    public class NewBehaviorScript : MonoBehavior
    {
        private int myVariable = 1;

        private void myMethod()
        {

        }
    } 

Upvotes: 2

panoskarajohn
panoskarajohn

Reputation: 1988

You need to wrap your code inside your class.

public class NewBehaviorScript : MonoBehaviour
{
   public float forwardForce = 2000f;
   ...etc
}

You can learn more about namespaces here https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/namespaces/

Your code is like this

public class NewBehaviorScript : MonoBehaviour
    { }
       public float forwardForce = 2000f;
       ...etc

Outside your class is the namespace in your case, this is why your compiler complains.

Upvotes: 4

Captain Kenpachi
Captain Kenpachi

Reputation: 7215

You have added a property or field outside of a class. Probably by mistake. e.g.

namespace MyNamespace
{
    //this is invalid because properties MUST be inside a class
    public string PropertyOne {get;set;}

    public class MyClass
    {
        public string PropertyTwo {get;set;}
    }
}

Upvotes: 1

Related Questions