CS0106, how would i solve this

Here is the code

public class PlayerController : MonoBehaviour
{
    public float moveSpeed;

    private bool isMoving;

    private Vector2 input;

    private Animator animator;

    public LayerMask solidObjectsLayer;
    public LayerMask interactableLayer;

    private void Awake()
    {
        animator = GetComponent<Animator>();
    }

    private void Update()
    {
        if (!isMoving)
        {
            input.x = Input.GetAxisRaw("Horizontal");
            input.y = Input.GetAxisRaw("Vertical");

            if (input.x != 0) input.y = 0;

            if (input != Vector2.zero)
            {
                animator.SetFloat("moveX", input.x);
                animator.SetFloat("moveY", input.y);

                Vector3 targetPos = transform.position;
                targetPos.x += input.x * moveSpeed * Time.deltaTime;
                targetPos.y += input.y * moveSpeed * Time.deltaTime;

                if (IsWalkable(targetPos)) ;

                StartCoroutine(Move(targetPos));
            }

        }

        animator.SetBool("isMoving", isMoving);

        if (Input.GetKeyDown(KeyCode.Z))
            Interact();
    }

    void Interact()
    {
        var facingDir = new Vector3(animator.GetFloat("moveX"), animator.GetFloat("moveY"));
        var interactPos = transform.position + facingDir;

        //Debug.DrawLine(transform.position, interactPos, Color.red, 1f);

        var collider = Physics2D.OverlapCircle(interactPos, 0.2f, interactableLayer);
        if (collider != null)
        {
            collider.GetComponent<Interactable>()?.Interact();
        }


        IEnumerator Move(Vector3 targetPos)
        {
            isMoving = true;

            while ((targetPos - transform.position).sqrMagnitude > Mathf.Epsilon)
            {
                transform.position = Vector3.MoveTowards(transform.position, targetPos, moveSpeed * Time.deltaTime);
                yield return null;
            }
            transform.position = targetPos;

            isMoving = false;
        }

        private bool IsWalkable(Vector3 targetPos)
        {
            // Check for collider overlaps within a circle centered at targetPos with a radius of 0.2f
            // solidObjectsLayer and interactableLayer are assumed to be defined elsewhere in the class
            if (Physics2D.OverlapCircle(targetPos, 0.2f, solidObjectsLayer | interactableLayer) != null)
            {
                // If an overlap is found, indicating that the position is not walkable, return false
                return false;
            }
            // If no overlap is found, return true indicating that the position is walkable
            return true;
        }
    }
}

The error is saying that private and public don’t go with the IsWalkable bool.

Is it possible that the above code is contained in some sort of local scope rather than at the top level of the class?

Oh ok ill add the whole doc code ok

there you go, i think this is what you meant

1 Answer

1

You are defining private bool IsWalkable from inside of the void Interact function. While this is technically allowed in C#, I expect this is not what you intended.

I tried what you said but it saidly just didn't work, it has to be inside void interact, as it takes values from Iemumerator

If they have to be inside of the Interact function then you can just remove "private" from the method definition. But if the only reason they are inside of the Interact function is to use some of the local variables, then couldn't you pass those variables in as parameters?

Yes Exactly so how would i pass those variables as parameters

What variables are you referring to? Move() only uses targetPos (which is already passed in as a parameter) and global variables. IsWalkable also only uses targetPos and global variables. Neither of these use any of the parameters defined inside of the Interact function. Also, functions defined within a function can only be called by that function, so I am surprised you are able to call either of those from Update().

Ok so when i do put it out side of Interact() this error happens for ever variable Assets\Scripts\PlayerController.cs(79,29): error CS0246: The type or namespace name 'Vector3' could not be found (are you missing a using directive or an assembly reference?)