Search code examples
xcodeunity-game-enginegame-physicspublicpublic-key

Code Error. The modifier public is not valid. Why?


I am making a 2D game in Unity and am trying to make my moveable character stop every time dialogue appears on screen.

I am using the Fungus extension for my dialogue as I'm a newbie to coding. Every thing I try however I run in to problems.

My current issue is that the modifier 'public' is not valid for this item.

Anyone know how this can be fixed? I have attached the code below. I assume the issue is with the public void CantMove() and public void CanMove() lines.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float moveSpeed;
    public Rigidbody2D theRB;
    public float jumpForce;

    private bool isGrounded;
    public Transform groundCheckPoint;
    public LayerMask whatIsGround;

    private bool canDoubleJump;

    private bool canMove = true;

    private Animator anim;
    private SpriteRenderer theSR;

    // Start is called before the first frame update
    void Start()
    {
        anim = GetComponent<Animator>();
        theSR = GetComponent<SpriteRenderer>();
    }

    // Update is called once per frame
    void Update()
    {
        if(!canMove)
        {
            theRB.velocity = new Vector2(0, 0);
        }
        else
        {
            theRB.velocity = new Vector2(moveSpeed * Input.GetAxis("Horizontal"), theRB.velocity.y);
        }

        public void CantMove()
        {
            canMove = false;
        }
        public void CanMove()
        {
            canMove = true;
        }

       //theRB.velocity = new Vector2(moveSpeed * Input.GetAxis("Horizontal"), theRB.velocity.y);

       isGrounded = Physics2D.OverlapCircle(groundCheckPoint.position, .2f, whatIsGround);

       if(isGrounded)
       {
            canDoubleJump = true;
        }

       if(Input.GetButtonDown("Jump"))
       {
            if (isGrounded)
            {
                theRB.velocity = new Vector2(theRB.velocity.x, jumpForce);
            }
            else
            {
                if(canDoubleJump)
                {
                    theRB.velocity = new Vector2(theRB.velocity.x, jumpForce);
                    canDoubleJump = false;
                }
            }
        }

        if(theRB.velocity.x > 0)
        {
            theSR.flipX = true;
        }   else if(theRB.velocity.x < 0)
        {
            theSR.flipX = false;
        }

        anim.SetFloat("moveSpeed", Mathf.Abs( theRB.velocity.x));
        anim.SetBool("isGrounded", isGrounded);
    }
}

'''


Solution

  • Your problem is that your two functions defined for CanMove and CantMove are declared inside of the Update function body... which makes them locally scoped functions which means they can never have public access and can only be called from within the Update function itself.

    Move these two functions outside of the Update function body like this...

    void Update() {
       ...
    }
    
    public void CantMove() {
        canMove = false;
    }
    
    public void CanMove() {
        canMove = true;
    }