简体   繁体   English

无法将脚本作为组件添加到 GameObject

[英]Cannot add script to a GameObject as a component

VS 截屏

This is my full script这是我的完整脚本

I get an error on line 15, beginning at the "PlayerControls" word.我在第 15 行收到一个错误,从“PlayerControls”字样开始。

error CS0246: The type or namespace name 'PlayerControls' could not be found (are you missing a using directive or an assembly reference?)错误 CS0246:找不到类型或命名空间名称“PlayerControls”(是否缺少 using 指令或程序集引用?)

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

namespace puggy
{
    public class InputHandler : MonoBehaviour
    {
        public float horizontal;
        public float vertical;
        public float moveAmount;
        public float mouseX;
        public float mouseY;

        PlayerControls inputActions;

        Vector2 movementInput;
        Vector2 cameraInput;

        public void OnEnable()
        {
            if (inputActions == null)
            {
                inputActions = new PlayerControls();
                inputActions.PlayerMovement.Movement.performed += inputActions => movementInput = inputActions.ReadValue<Vector2>();
                inputActions.PlayerMovement.Camera.performed += i => cameraInput = i.ReadValue<Vector2>();
            }

            inputActions.Enable();

        }

        private void OnDisable()
        {
            inputActions.Disable();
        }

        public void TickInput(float delta)
        {
            MoveInput(delta);
        }

        private void MoveInput(float delta)
        {
            horizontal = movementInput.x;
            vertical = movementInput.y;
            moveAmount = Mathf.Clamp01(Mathf.Abs(horizontal) + Mathf.Abs(vertical));
            mouseX = cameraInput.x;
            mouseY = cameraInput.y;
        }
    }
}

I'm guessing it wants a reference for "PlayerControls".我猜它需要“PlayerControls”的参考。 I'm confused though because I followed a walkthrough, I ran into other issues but was able to resolve them pretty quickly.我很困惑,因为我遵循了一个演练,我遇到了其他问题,但能够很快解决它们。 This one is tripping me up though.不过,这个让我绊倒了。

在此处输入图像描述

I'm expecting to be able to attach this script to my player model and have it move based on player input.我希望能够将此脚本附加到我的播放器 model 并让它根据播放器输入移动。

I have re-typed my script and followed other forums to see if anyone else has had similar issues.我重新输入了我的脚本并关注了其他论坛,看看是否有其他人遇到过类似的问题。

EDIT:编辑:

玩家控制

public class PlayerControls has to be in a file called PlayerControls.cs ! public class PlayerControls必须位于名为PlayerControls.cs的文件中!

Looking at your screenshot you have InputHandler in a file called PlayerControls.cs查看您的屏幕截图,您在名为PlayerControls.cs的文件中有InputHandler

I guess you copied/renamed the class or file.我猜你复制/重命名了 class 或文件。 Please make sure Filename and Classname match!请确保文件名和类名匹配!

I figured it out.我想到了。 When making the "PlayerControls Input Action" I forgot to also click "generate C# class".在制作“PlayerControls Input Action”时,我忘记也单击“generate C# class”。

This makes sure that all the stuff I set up in the GUI, is applied into the C# script.这确保我在 GUI 中设置的所有内容都应用到 C# 脚本中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM