【问题标题】:Can't add script to GameObject无法将脚本添加到游戏对象
【发布时间】:2021-11-24 10:42:54
【问题描述】:

在尝试将脚本添加到游戏对象时,我反复收到错误消息。到目前为止,我已经尝试将 MonoBehavior 类名称更改为脚本名称,并检查了我的代码是否有错误,尽管终端中没有显示任何错误。这是我的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace LE
{
public class InputHandler : MonoBehaviour
{
public float horizontal;
public float vertical;
public float moveAmountl;
public float mouseX;
public float mouseY

    PlayerControls inputActions;

    Vector2 movementInput;
    Vector2 cameraInput

    public void private 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 private void OnDisable()
     {
        inputActions.Disable();
    }
    }

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

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

【问题讨论】:

  • 错误是什么?

标签: c# unity3d game-development


【解决方案1】:

可能是这样(注意输入了两次 private 和 public void):

 public void private void OnEnable() {

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

应该是

 private void OnEnable() {

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

【讨论】:

  • 啊,谢谢。我会试试看。我不敢相信我做到了。
  • 嗯。我这样做了,最后得到了同样的错误“无法添加脚本”“无法添加脚本组件‘inputHandler’,因为找不到脚本类。确保没有编译错误并且文件名和类名匹配”
  • 您的脚本是否名为“InputHandler.cs”(注意:区分大小写)
  • 啊,我可以从你的错误中看出,如果是复制粘贴,你已经用一个小“i”调用了脚本本身“inputHandler.cs” - 检查一下。
  • 感谢您的帮助,原来如此。我应该怎么做才能修复它?更改公共类和统一脚本名称或在 Visual Studio 中更改?
猜你喜欢
  • 2020-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多