代码错误。修饰符 public 无效。为什么?

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

我正在 Unity 中制作 2D 游戏,并试图在每次屏幕上出现对话时让我的可移动角色停止。

我在对话中使用 Fungus 扩展,因为我是编码新手。我尝试的每件事都 运行 遇到了问题。

我当前的问题是修饰符 'public' 对该项目无效。

有人知道如何解决这个问题吗?我附上了下面的代码。我认为问题出在 public void CantMove()public void CanMove() 行。

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);
    }
}

'''

你的问题是你为 CanMove 和 CantMove 定义的两个函数是在 Update 函数体内声明的...这使它们成为局部范围的函数,这意味着它们永远不能 public 访问并且只能是从更新函数本身调用。

像这样将这两个函数移到 Update 函数体之外...

void Update() {
   ...
}

public void CantMove() {
    canMove = false;
}

public void CanMove() {
    canMove = true;
}