unity3d 错误cs0246“找不到类型或命名空间名称'Player'(是否缺少using指令或程序集引用?)”

ezykj2lf  于 2023-02-09  发布在  其他
关注(0)|答案(1)|浏览(645)

我正在使用Unity创建一个自上而下的2d游戏,我一直收到错误Error cs0246“类型或命名空间名称'Player'无法找到(您是否缺少using指令或汇编引用?)似乎我已经找遍了所有地方,但我无法修复它,因为我是Unity的新手。这是代码

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

public class PlayerController : MonoBehaviour{

        public int playerId = 0;
        public Animator animator;
        public GameObject crosshair7;   

        private Player player;

        void Awake() {
            player = ReInput.players.GetPlayer(playerId);
        }

        void Update()
        {

            Vector3 movement = newVector3(Input.GetAxis("MoveHorizontal"),Input.GetAxis("MoveVertical"), 0.0f);

            if(player.GetButton("Fire")) {
                 Debug.Log("Fire");
            }

            Movecrosshair7();

            animator.SetFloat("Horizontal", movement.x);
            animator.SetFloat("Vertical", movement.y);
            animator.SetFloat("Magnitude", movement.magnitude);

            transform.position = transform.position + movement * Time.deltaTime;
        }
        private void Movecrosshair7() {
            Vector3 aim = new Vector3(player.GetAxis("AimHorizontal"), player.GetAxis("AimVertical"), 0.0f);

            if (aim.magnitude > 0.0f) {
                aim.Normalize();
                aim *= 0.04f;
                crosshair7.transform.localPosition = aim;
            }
        }
    }
6ie5vjzr

6ie5vjzr1#

您的错误是因为您在另一个命名空间中创建了名为Player的类,但尚未导入该类。如果您使用的是Visual Studio,则可以对带红色下划线的private Player;执行ctrl+.操作以自动导入该类,否则将您的命名空间添加到文件顶部:

using My.Players.Namespace;

我注意到你的代码中还有一个错误:

Vector3 movement = newVector3(Input.GetAxis("MoveHorizontal"),Input.GetAxis("MoveVertical"), 0.0f);

应该是

Vector3 movement = new Vector3(Input.GetAxis("MoveHorizontal"), Input.GetAxis("MoveVertical"), 0.0f);

相关问题