【问题标题】:Error cs0246 "The type or namespace name 'Player' could not be found (are you missing a using directive or an assembly reference?)"错误 cs0246“找不到类型或命名空间名称‘Player’(您是否缺少 using 指令或程序集引用?)”
【发布时间】:2021-08-28 01:40:35
【问题描述】:

我正在使用 unity 创建一个自上而下的 2d 游戏,但我不断收到错误错误 cs0246“找不到类型或命名空间名称'Player'(您是否缺少 using 指令或程序集引用?)它好像我到处都看过,但我无法修复它,因为我是统一的新手。这是代码

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

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    您的错误是因为您在另一个命名空间中创建了一个名为 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);
    

    【讨论】:

    • 成功了,谢谢,我对unity和c#很陌生
    • 不用担心。如果有帮助,请不要忘记将答案标记为已接受! :)
    猜你喜欢
    • 2022-01-20
    • 2019-12-02
    • 1970-01-01
    • 1970-01-01
    • 2021-08-20
    • 2020-02-19
    • 1970-01-01
    • 2022-06-15
    • 2020-08-29
    相关资源
    最近更新 更多