A namespace cannot directly contain members such as fields or methods error

This is my code, I was following along the tutorial as best as I could. I Googled and it said basically you can’t have stuff in-between namespaces, but I am not doing so AFAIK.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using RPG.Movement;

namespace RPG.Control {

    public class PlayerController : MonoBehaviour {
    }
    

    private void Update()
    {
        if (Input.GetMouseButton(0))
        {
            MoveToCursor();
        }
    }

    private void MoveToCursor()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        bool hasHit = Physics.Raycast(ray, out hit);
        if (hasHit)
        {
            GetComponent<Mover>().MoveTo(hit.point);
        }
    }
}

The closing bracket underneath this statement is saying that this is the sum and total of the class.
You’ll need to remove that bracket and put another bracket before the last bracket in the script to put the methods and fields in the PlayerController class and close it out.

I figured it was a formatting error. I still get this.

Assets\Scripts\PlayerController.cs(54,2): error CS1513: } expected

I understand what you mean though.

I know the problem is below but I can’t figure out how to solve it. I’ve done it before.

This is the new error.

Assets\Scripts\PlayerController.cs(27,30): error CS0246: The type or namespace name ‘Fighter’ could not be found (are you missing a using directive or an assembly reference?)

namespace RPG.Control {

    public class PlayerController : MonoBehaviour {
    
    
    private void Update()
    {
       InteractWithCombat();
       InteractWithMovement();
    }

    private void InteractWithCombat()
    {
        RaycastHit[] hits = Physics.RaycastAll(GetMouseRay());
        foreach (RaycastHit hit in hits)
        {
            CombatTarget target = hit.transform.GetComponent<CombatTarget>();
            if (target == null) continue;

            if (Input.GetMouseButtonDown(0))
            {
                GetComponent<Fighter>().Attack(target);
            }
        }
    }

    private void InteractWithMovement()
    {
        if (Input.GetMouseButton(0))
        {
            MoveToCursor();
        }
    }

    private void MoveToCursor()
    {
        RaycastHit hit;
        bool hasHit = Physics.Raycast(GetMouseRay(), out hit);
        if (hasHit)
        {
            GetComponent<Mover>().MoveTo(hit.point);
        }
    }
    
    private static Ray GetMouseRay()
    {
        return Camera.main.ScreenPointToRay(Input.mousePosition);
    }
    }
}

The compiler is telling you that it doesn’t know what a Fighter is in this context…
Do you have

using RPG.Combat;

in your usings directives at the top of the script?

I fixed that, I get this now.

Assets\Scripts\PlayerController.cs(14,11): error CS1003: Syntax error, ‘(’ expectedAssets\Scripts\PlayerController.cs(14,11): error CS1003: Syntax error, ‘(’ expected
Assets\Scripts\PlayerController.cs(14,32): error CS1026: ) expected

Paste your current script (the whole script) in again (as line 14 won’t be the same anymore).
Before pasting in the script, type three backwards apostrophes on a new line, then on the next line press paste.
example:
```
Some Code
```
becomes

Some Code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using RPG.Movement;
using RPG.Combat;

namespace RPG.Control {

    public class PlayerController : MonoBehaviour {
    
    
    private void Update()
    {
       if InteractWithCombat() return;
       if InteractWithMovement(() return;
       print("Nothing to do");
    }

    private bool InteractWithCombat()
    {
        RaycastHit[] hits = Physics.RaycastAll(GetMouseRay());
        foreach (RaycastHit hit in hits)
        {
            CombatTarget target = hit.transform.GetComponent<CombatTarget>();
            if (target == null) continue;

            if (Input.GetMouseButtonDown(0))
            {
                GetComponent<Fighter>().Attack(target);
            }
             return true;
        }
        return false;
    }

    private bool InteractWithMovement()
    {
        RaycastHit hit;
        bool hasHit = Physics.Raycast(GetMouseRay(), out hit);
        if (hasHit)
        {
            if (Input.GetMouseButton(0))
            {
                GetComponent<Mover>().MoveTo(hit.point);
            }
            return true;
        }
        return false;
    }
    
    private static Ray GetMouseRay()
    {
        return Camera.main.ScreenPointToRay(Input.mousePosition);
    }
    }
}

I edited the post for you. The ` you are looking for is the backwards apostrophe next to the 1 key on your keyboard.

Both of these lines have some structural issues…
An if statement must start and end with a parenthesis… These should look like this:

if (InteractWithCombat()) return;
if (InteractWithMovement()) return;

The error message was trying to tell you that after the if keyword, you must have a (

{

    public class PlayerController : MonoBehaviour {
    
    
    private void Update()
    {
            if (InteractWithCombat()) return;
            if (InteractWithMovement()) return;
            print("Nothing to do");
    }

    private bool InteractWithCombat()
    {
        RaycastHit[] hits = Physics.RaycastAll(GetMouseRay());
            foreach (RaycastHit hit in hits)
            {
                CombatTarget target = hit.transform.GetComponent<CombatTarget>();
                if (target == null) continue;
            }
            if (Input.GetMouseButtonDown(0))
            {
                GetComponent<Fighter>().Attack(target);
            }
             return true;
        }
        return false;
    }

    private bool InteractWithMovement()
    {
        RaycastHit hit;
        bool hasHit = Physics.Raycast(GetMouseRay(), out hit);
        if (hasHit)
        {
            if (Input.GetMouseButton(0))
            {
                GetComponent<Mover>().MoveTo(hit.point);
            }
            return true;
        }
        return false;
    }
    
    private static Ray GetMouseRay()
    {
        return Camera.main.ScreenPointToRay(Input.mousePosition);
    }
    }
}

Now I get the red lines and more errors. I’m trying to learn what is causing the errors.

I can’t seem to print screen and paste here?

I believe the error (which is causing multiple errors) is an extra } in this method. See my annotations.

Do you have prior experience with Unity or the C# language?

Yes, I have done Unity Learn and a course in C. I’m more of a hobbyist programmer at this point though.

Also, I have a long-term flu that is making me groggy. I am usually a sharper cookie. THanks for your help!

Now I get this error :

Assets\Scripts\Combat\Fighter.cs(27,2): error CS1513: } expected.

I’ve noticed when this error comes up it’s a {} issue, but that simply putting a } does not solve it?

This version of the script is correct. I think what happened in the 2nd version you posted is that you added an extra brace at the 2nd 2…

Assets\Scripts\Combat\Fighter.cs(45,2): error CS1513: } expected

I still get this error, I pasted exactly as above.

Is there a simple way to check these errors like Rainbow Carets? They waste a lot of time. I’m generally sure I did it right.

I cut and pasted from the commit and I got even more errors!

using System;
using RPG.Combat;
using RPG.Movement;
using UnityEngine;
namespace RPG.Control
{
    public class PlayerController : MonoBehaviour
    {

        private void Update()
        {
            InteractWithCombat();
            InteractWithMovement();
            if (InteractWithCombat()) return;
            if (InteractWithMovement()) return;
        }

        private void InteractWithCombat()
        private bool InteractWithCombat()
        {
            RaycastHit[] hits = Physics.RaycastAll(GetMouseRay());
            foreach (RaycastHit hit in hits)
            {
                CombatTarget target = hit.transform.GetComponent<CombatTarget>();
                if (target == null) continue;
                if (Input.GetMouseButtonDown(0))
                {
                    GetComponent<Fighter>().Attack(target);
                }
                return true;
            }
            return false;
        }

        private void InteractWithMovement()
        {
            if (Input.GetMouseButton(0))
            {
                MoveToCursor();
            }
        }

        private void MoveToCursor()
        private bool InteractWithMovement()
        { 
            RaycastHit hit;
            bool hasHit = Physics.Raycast(GetMouseRay(), out hit);
            if (hasHit)
            {
                GetComponent<Mover>().MoveTo(hit.point);
                if (Input.GetMouseButton(0))
                {
                    GetComponent<Mover>().MoveTo(hit.point);
                }
                return true;
            }
            return false;
        }

        private static Ray GetMouseRay()
        {
            return Camera.main.ScreenPointToRay(Input.mousePosition);
        }
    }
}

---

From Implementing Action Priority

It looks like you copied from the Diff (which is what we see when we go to the commit)… You’ll notice it has a lot of + and - on the left hand side which aren’t copied. There were many areas that were removed (the - sign next to the line) and sections that were added (the + sign next to the line).
Here’s the corrected script:

using System;
using RPG.Combat;
using RPG.Movement;
using UnityEngine;
namespace RPG.Control
{
    public class PlayerController : MonoBehaviour
    {

        private void Update()
        {
            if (InteractWithCombat()) return;
            if (InteractWithMovement()) return;
        }


        private bool InteractWithCombat()
        {
            RaycastHit[] hits = Physics.RaycastAll(GetMouseRay());
            foreach (RaycastHit hit in hits)
            {
                CombatTarget target = hit.transform.GetComponent<CombatTarget>();
                if (target == null) continue;
                if (Input.GetMouseButtonDown(0))
                {
                    GetComponent<Fighter>().Attack(target);
                }
                return true;
            }
            return false;
        }




        private bool InteractWithMovement()
        { 
            RaycastHit hit;
            bool hasHit = Physics.Raycast(GetMouseRay(), out hit);
            if (hasHit)
            {
                GetComponent<Mover>().MoveTo(hit.point);
                if (Input.GetMouseButton(0))
                {
                    GetComponent<Mover>().MoveTo(hit.point);
                }
                return true;
            }
            return false;
        }

        private static Ray GetMouseRay()
        {
            return Camera.main.ScreenPointToRay(Input.mousePosition);
        }
    }
}


1 Like

Privacy & Terms