【问题标题】:error CS0246: The type or namespace name 'PlayerMotor' could not be found错误 CS0246:找不到类型或命名空间名称“PlayerMotor”
【发布时间】:2019-09-14 19:39:07
【问题描述】:

我正在统一制作我的第一个游戏,但出现了这个错误我不知道我是否可以使用“使用”标签来解决这个问题,我不知道是哪个

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

    [RequireComponent(typeof(PlayerMotor))]
    public class PlayerController : MonoBehaviour
    {
        public LayerMask ourMovmentMask;

        Camera cam;
        PlayerMotor motor;
        // Start is called before the first frame update
        void Start()
        {
            cam = Camera.main;
            motor = GetComponent<PlayerMotor>();
        }

        // Update is called once per frame
        void Update()
        {
            if (Input.GetMouseButtonDown(0))
            {
                Ray ray = cam.ScreenPointToRay(Input.mousePosition);
                RaycastHit hit;
                if (Physics.Raycast(ray, out hit, 100, ourMovmentMask))
                {
                    Debug.Log("we hit " + hit.collider.name + " " + hit.point);
                    // move player to what we hit

                    // stop focusing any objects
                }
            }
        }
    }

【问题讨论】:

  • 你需要将你的类封装在一个命名空间中...
  • 您可以将 using 语句放在命名空间 See this StackOverflow question asking about the issue 内。很多时候,在 Unity 中创建的脚本不会包含命名空间。
  • @TheIncorrigible1 ...我不知道为什么这么多人赞成,但你在说什么?您需要在命名空间中封装某些东西的唯一情况是,当您想要隐藏它或者同名的东西已经存在于另一个命名空间中时。这里的问题是甚至找不到类型......
  • @Eliasar 问题似乎不在于 where 放置 using 语句,而是因为缺少 PlayerMotor 类型甚至找不到using 语句(无论是否在命名空间内)或者它不存在可能是由于编译器错误等
  • 你能告诉我们PlayerMotor是什么吗?你实现了这个还是做了它,例如来自下载的资产?如果您将所有关于 motor 的行注释掉,Unity 控制台中是否还有其他编译器错误?

标签: c# unity3d


【解决方案1】:

您需要游戏对象具有“PlayerMotor”组件。

您是否定义了 PlayerMotor 类?如果没有则定义它,因为编译器找不到该类。

如果是,则检查它是否在您的项目目录中

【讨论】:

    猜你喜欢
    • 2019-07-14
    • 2013-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-25
    相关资源
    最近更新 更多