求助:Unity出现Error CS0426错误,'OnFootActions'在PlayerInput中不存在
解决Error CS0426: The type name 'OnFootActions' does not exist in the type 'PlayerInput'
这个错误的核心原因是:OnFootActions并非Unity内置PlayerInput类的嵌套类型,它是需要通过Input Action Asset自定义生成的动作映射类。以下是具体解决步骤:
1. 创建并配置Input Action Asset
- 在Unity编辑器项目面板右键 →
Create→Input Actions,新建一个Input Action资源(建议命名为PlayerControls,和教程保持一致)。 - 打开该资源,添加名为
OnFoot的Action Map,再在其中创建Movement等需要的动作(动作配置需匹配教程中的设置)。 - 保存资源后,在Inspector面板点击
Generate C# Class按钮,Unity会自动生成对应类文件(类名与资源名一致)。
2. 修正代码错误
将原代码中的PlayerInput替换为生成的类名,同时修正变量实例化的大小写错误,以下是修正后的示例(假设生成的类为PlayerControls):
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem; public class InputManager : MonoBehaviour // 修正原拼写错误:InputManger → InputManager { private PlayerControls playerControls; private PlayerControls.OnFootActions onFoot; private PlayerMotor motor; void Awake() { playerControls = new PlayerControls(); onFoot = playerControls.OnFoot; motor = GetComponent<PlayerMotor>(); } void FixedUpdate() { motor.ProcessMove(onFoot.Movement.ReadValue<Vector2>()); } private void OnEnable() { onFoot.Enable(); } private void OnDisable() { onFoot.Disable(); } }
额外检查项
- 确认Unity的Input System包已安装:打开
Window→Package Manager,搜索Input System并完成安装(若未安装)。 - 检查生成的C#类文件无编译错误,且已正确导入项目。
内容的提问来源于stack exchange,提问作者fox100
相关产品推荐
相关产品推荐

